浮点精度未格式化为字符串

Ali*_*ner 2 c++ string format precision

当我编写一个简单的函数并使用动词表示法wprintf向其传递一个双精度数字时,它只是在屏幕上打印一个“ ”,而不是像格式中那样打印数字。floatL"%.2f"f0.00

我需要一些帮助,因为我到处都看到它只是说L"%.2f"是打印具有两位小数精度的数字的方法。

float duration = (float)((std::clock() - mTimer) / (float)(CLOCKS_PER_SEC / 1000));
wsprintf(message, L">> Speed: %.2f\r\n", duration);
Run Code Online (Sandbox Code Playgroud)

这些是引起我头痛的两条线......它们导致

>> Speed: f
Run Code Online (Sandbox Code Playgroud)

正在控制台上打印。

我正在寻找的输出是这样的:

>> Speed: 4000.00
Run Code Online (Sandbox Code Playgroud)

P.W*_*P.W 5

该函数wsprintf()(似乎是 Windows 特定的)不支持浮点参数,您可以改为使用 for swprintf()(这是标准库函数)。

此外,文档中有一条注释wsprintf指出:

不使用。考虑改用以下函数之一:StringCbPrintf, StringCbPrintfEx, StringCchPrintf, or StringCchPrintfEx。请参阅安全注意事项。