数字在编辑框中总是输出为十进制,无论它是 int 还是 double
Number always outputs as decimal in edit box, regardless if it is an int or a double
我对编码还很陌生,过去几天我一直在探索 WINAPI 并尝试学习新东西,所以我决定制作一个计算器(老实说,这比我预期的要难)。
现在一切正常,但剩下的唯一问题是当我尝试使用计算器时,我总是得到如下所示的结果:22+22 = 44.0000000。有没有一种方法可以将编辑框格式化为仅在需要时显示小数,或者我在函数中做错了什么?
我在下面包含了一个计算函数(用于乘法):
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);
}
当您使用 std::to_string()
功能时,自定义格式实际上不可用。相反,您可以使用标准格式化输出运算符将数据写入字符串流,然后从中读取 std::string
:
// 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();
(请注意,您需要 #include <sstream>
。)
double
类型的默认输出格式类似于使用 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。
我对编码还很陌生,过去几天我一直在探索 WINAPI 并尝试学习新东西,所以我决定制作一个计算器(老实说,这比我预期的要难)。
现在一切正常,但剩下的唯一问题是当我尝试使用计算器时,我总是得到如下所示的结果:22+22 = 44.0000000。有没有一种方法可以将编辑框格式化为仅在需要时显示小数,或者我在函数中做错了什么?
我在下面包含了一个计算函数(用于乘法):
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);
}
当您使用 std::to_string()
功能时,自定义格式实际上不可用。相反,您可以使用标准格式化输出运算符将数据写入字符串流,然后从中读取 std::string
:
// 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();
(请注意,您需要 #include <sstream>
。)
double
类型的默认输出格式类似于使用 printf()
function; however, you can add further customizations – should you so desire – using the various functions in the <iomanip>
standard header。