I'm pretty new to coding and for the last couple of days I've been exploring WINAPI and trying to learn something new, so I decided to make a calculator (honestly, was harder than I anticipated).
Now I've got everything working, but the only problem left is that when I try to use the calculator, I always get a result that looks like this: 22 22 = 44.0000000. Is there a way I could maybe format the Edit box to show decimal numbers only when needed or maybe there is something I have done wrong in the function?
I included one of the computing functions (used for multiplication) below:
void multiply() {
WCHAR A[20], B[20];
GetWindowText(hOutputBot, A, 20);
GetWindowText(hOutputTop, B, 20);
double Aa = std::stof(A);
double Bb = std::stof(B);
double Res = Bb * Aa;
std::wstring s = std::to_wstring(Res);
LPCWSTR str = s.c_str();
SetWindowTextW(hOutputBot, str);
}
CodePudding user response:
Custom formatting is not really available when you use the std::to_string()
function. Instead, what you can do is to use the standard formatted output operator to write your data to a string stream, then read your std::string
from that:
// std::wstring s = std::to_wstring(Res);
std::wstringstream ss; ss << Res; // Write value to string stream
std::wstring s; ss >> s; // ... then read it into string.
//...
LPCWSTR str = s.c_str();
(Note that you will need to #include <sstream>
.)
The default format of the output of a double
type is similar to using the %lg
format specifier in the C-style printf()
function; however, you can add further customizations – should you so desire – using the various functions in the <iomanip>
standard header.