2

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);
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Redas
  • 68
  • 1
  • 6

1 Answers1

0

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.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • [std::to_wstring](https://en.cppreference.com/w/cpp/string/basic_string/to_wstring) does provide overloads. If you choose one of the overloads that accepts an integer value you *do* get integer formatting. Even if that is not what you are looking for, there's [std::format](https://en.cppreference.com/w/cpp/utility/format/format) (starting with C++), that's **far** superior in just about any aspect to the clunky stringstream interface. – IInspectable Aug 30 '21 at 13:17
  • @IInspectable (1) OP doesn't have an integer - just a double that happens to have a zero fractional part. (2) `std::format` is nice (if a little tricky to get used to) but only if you have a compiler that supports it. – Adrian Mole Aug 30 '21 at 13:21
  • (1) Er, that's a `static_cast` away. (2) MSVC supports the entire C++20 library. – IInspectable Aug 30 '21 at 14:04
  • @IInspectable But you're missing the issue with (1). The OP doesn't know in advance if the value will have a fractional part or not. It's just a `double` result of a multiplication of two other `double` values. They only want to display it without `.000000` if that's appropriate. – Adrian Mole Aug 30 '21 at 14:08
  • Leaving the decision-making to the formatting function seems rather questionable. Indeed, I hadn't even considered that to be an option. Now, ideally, the code should already know, whether it is dealing with an integer or fractional value. In case it doesn't, it can check: `bool is_fractional(double x) { return floor(x) != x; }`. – IInspectable Aug 30 '21 at 14:29