printf() 中宽度小于精度有什么意义?
我遇到了一些代码,其中一行看起来像:
fprintf(fd, "%4.8f", ptr->myFlt);
这些天没有太多使用 C++,我阅读了有关 printf 及其同类的文档,并了解到在这种情况下 4 是“宽度”,8 是“精度”。宽度定义为输出占用的最小空间数,如果需要,可以用前导空格填充。
既然如此,我无法理解像“%4.8f”这样的模板有什么意义, 因为该点后面的 8 位(必要时补零)小数已经可以确保满足并超过 4 的宽度。因此,我在 Visual C++ 中编写了一个小程序:
// Formatting width test
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
printf("Need width when decimals are smaller: >%4.1f<\n", 3.4567);
printf("Seems unnecessary when decimals are greater: >%4.8f<\n", 3.4567);
printf("Doesn't matter if argument has no decimal places: >%4.8f<\n", (float)3);
return 0;
}
它给出了以下输出:
Need width when decimals are smaller: > 3.5<
Seems unnecessary when decimals are greater: >3.45670000<
Doesn't matter if argument has no decimal places: >3.00000000<
在第一种情况下,精度小于指定的宽度,并且实际上添加了前导空格。然而,当精度更高时,宽度似乎是多余的。
有这样的格式的原因吗?
I came across some code with a line looking like:
fprintf(fd, "%4.8f", ptr->myFlt);
Not working with C++ much these days, I read the doc on printf and its ilk, and learned that in this case 4 is the "width", and 8 is the "precision". Width was defined as the minimum number of spaces occupied by the output, padding with leading blanks if need be.
That being the case, I can't understand what the point of a template like "%4.8f" would be,
since the 8 (zero-padded if necessary) decimals after the point would already ensure that the width of 4 was met and exceeded. So, I wrote a little program, in Visual C++:
// Formatting width test
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
printf("Need width when decimals are smaller: >%4.1f<\n", 3.4567);
printf("Seems unnecessary when decimals are greater: >%4.8f<\n", 3.4567);
printf("Doesn't matter if argument has no decimal places: >%4.8f<\n", (float)3);
return 0;
}
which gives the following output:
Need width when decimals are smaller: > 3.5<
Seems unnecessary when decimals are greater: >3.45670000<
Doesn't matter if argument has no decimal places: >3.00000000<
In the first case, the precision is less than width specified, and in fact a leading space is added. When the precision is greater, however, the width seems redundant.
Is there a reason for a format like that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅当打印数字的总宽度小于指定宽度时,宽度格式说明符才会影响输出。显然,当精度设置为大于或等于宽度时,这种情况永远不会发生。因此,在这种情况下宽度规范是没有用的。
这里来自 MSDN 的一篇文章;最后一句话解释了这一点。
The width format specifier only affects the output if the total width of the printed number is less than the specified width. Obviously, this can never happen when the precision is set greater than or equal to the width. So, the width specification is useless in this case.
Here's an article from MSDN; the last sentence explains it.
也许是程序员的错误?也许他们交换了
%8.4f
或者他们实际上想要%12.8f
甚至%012.8f
查看键盘示例:
输出
Perhaps a mistake of the programmer? Perhaps they swapped
%8.4f
or they actually intended%12.8f
or even%012.8f
See codepad sample:
Output
可能只是猜测,但是:
精度为小数指定了一个长度,如果您有更多的小数,则不会超过该长度。
同样,宽度可以防止您的号码占用比应有的空间更少的空间。
如果您想到某种带有数字的表格,则只有当每行的每一列具有相同的宽度(无论其包含多少数字)时,您才能实现统一的列。
因此,在某些价格(例如 10.00 欧元)格式中,您总是需要 2 位小数,因此需要精度。
对于您的特定行:我和您一样对这种特殊情况下宽度说明符的冗余有同样的感觉。
Probably just a guess, but:
The precision gives the decimals one length that wont be exceeded if you got more decimals.
Likewise the width prevents your number from consuming less space than it should.
If you think of some kind of table with numbers you only can achieve uniform columns when each column on each row has the same width regardless of the number it contains.
So precision would be needed in some price like format like 10.00€ where you always want 2 decimals.
For your specific line: I feel like you about the redundancy of the width specifier in this special case.