简体   繁体   English

浮点精度未在字符串中格式化

[英]Float precision not formatting in string

When I write a simple wprintf function and pass a double to it a float number using the verb notation L"%.2f" it simply prints an " f " on the screen instead of a number like in the format 0.00 . 当我编写一个简单的wprintf函数并使用动词符号L"%.2f"向其传递一个双float它只是在屏幕上打印了一个“ f ”,而不是像0.00这样的数字。

I would like some help as everywhere I look it simply says L"%.2f" is the way to print a number with 2 decimal digit precision. 我需要一些帮助,因为到处L"%.2f"是打印2位小数位精度的数字的方式。

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

These are the 2 lines causing my headache... they result in 这是引起我头痛的2条线...

 >> Speed: f 

being printed on the console. 在控制台上打印。

The output i'm looking for is this: 我正在寻找的输出是这样的:

 >> Speed: 4000.00 

The function wsprintf() (which seems to be windows specific) does not support floating point parameters, You can go for swprintf() (which is a standard library function) instead. 函数wsprintf() (似乎是特定于Windows的)不支持浮点参数,可以swprintf() (这是标准库函数)。

Moreover, there is a note in the wsprintf documentation which states: 此外, wsprintf文档中有一条注释指出:

Do not use. 不使用。 Consider using one of the following functions instead: StringCbPrintf, StringCbPrintfEx, StringCchPrintf, or StringCchPrintfEx . 考虑改用以下功能之一: StringCbPrintf, StringCbPrintfEx, StringCchPrintf, or StringCchPrintfEx See Security Considerations. 请参阅安全注意事项。

您可以做的是:

cout << fixed << setprecision(2) << message << ">> Speed : "<< duration << endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM