浮点精度未在字符串中格式化
Float precision not formatting in string
当我编写一个简单的wprintf
函数并使用动词表示法将一个float
数字传递给它时L"%.2f"
它只是在屏幕上打印一个" f
",而不是像格式那样0.00
的数字。
我希望得到一些帮助,因为无论我在哪里看,它都只是说L"%.2f"
是打印具有 2 个十进制数字精度的数字的方法。
float duration = (float)((std::clock() - mTimer) / (float)(CLOCKS_PER_SEC / 1000));
wsprintf(message, L">> Speed: %.2frn", duration);
这是引起我头痛的 2 行...它们导致
>> Speed: f
正在控制台上打印。
我正在寻找的输出是这样的:
>> Speed: 4000.00
函数wsprintf()
(似乎是特定于窗口的(不支持浮点参数,你可以改用swprintf()
(这是一个标准的库函数(。
此外,wsprintf
文档中有一个注释,其中指出:
请勿使用。请考虑改用以下函数之一:
StringCbPrintf, StringCbPrintfEx, StringCchPrintf, or StringCchPrintfEx
。请参阅安全注意事项。
你可以做的是:
cout << fixed << setprecision(2) << message << ">> Speed : "<< duration << endl;
相关文章:
- 如何从C++中的格式化字符串派生整数?
- C++逐步格式化字符串
- C++:格式化包含十六进制值的字符串
- 格式化返回字符串C++
- 有没有更快的方法从成员函数返回格式化字符串
- 如何在C++中对静态缓冲区执行字符串格式化?
- 格式化的时间字符串在解析为time_point时会产生小时差异
- C++ ctime() 迄今为止格式化的字符串?
- 自定义 {fmt} 格式化函数,具有编译时格式字符串检查功能
- 如何在提升库中格式化字符串
- 使用参数的可变大小向量格式化字符串(例如,将参数向量传递给 std::snprintf)
- 使用 c++ 格式化字符串的自然方法
- 使用此字符串重新格式化器使用什么代替 substr
- snprintf等效于WCHAR_T计算格式化的字符串大小(MAC)
- WXSTRING:是否有使用类似Python3的占位符实现字符串格式化的C/C 代码
- C字符串.格式化插入斜杠
- 如何截断字符串 [格式化] ?C++
- 是否为osx标准库实现swscanf子字符串格式化([a-z])
- c++字符串格式化-固定宽度的浮点数,以及在偏移处有空格的字符串
- 如何创建可变模板字符串格式化程序