printf() 中宽度小于精度有什么意义?

发布于 2024-11-19 03:30:02 字数 983 浏览 2 评论 0原文

我遇到了一些代码,其中一行看起来像:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木森分化 2024-11-26 03:30:02

仅当打印数字的总宽度小于指定宽度时,宽度格式说明符才会影响输出。显然,当精度设置为大于或等于宽度时,这种情况永远不会发生。因此,在这种情况下宽度规范是没有用的。

这里来自 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.

A nonexistent or small field width does not cause the truncation of a field; if the result of a conversion is wider than the field width, the field expands to contain the conversion result.

屌丝范 2024-11-26 03:30:02

也许是程序员的错误?也许他们交换了 %8.4f 或者他们实际上想要 %12.8f 甚至 %012.8f

查看键盘示例

#include <stdio.h>

int main()
{
    printf("Seems unnecessary when decimals are greater: >%4.8f<\n", 3.4567);
    printf("Seems unnecessary when decimals are greater: >%8.4f<\n", 3.4567);
    printf("Seems unnecessary when decimals are greater: >%12.4f<\n", 3.4567);
    printf("Seems unnecessary when decimals are greater: >%012.4f<\n", 3.4567);

    return 0;
}

输出

Seems unnecessary when decimals are greater: >3.45670000<
Seems unnecessary when decimals are greater: >  3.4567<
Seems unnecessary when decimals are greater: >      3.4567<
Seems unnecessary when decimals are greater: >0000003.4567<

Perhaps a mistake of the programmer? Perhaps they swapped %8.4f or they actually intended %12.8f or even %012.8f

See codepad sample:

#include <stdio.h>

int main()
{
    printf("Seems unnecessary when decimals are greater: >%4.8f<\n", 3.4567);
    printf("Seems unnecessary when decimals are greater: >%8.4f<\n", 3.4567);
    printf("Seems unnecessary when decimals are greater: >%12.4f<\n", 3.4567);
    printf("Seems unnecessary when decimals are greater: >%012.4f<\n", 3.4567);

    return 0;
}

Output

Seems unnecessary when decimals are greater: >3.45670000<
Seems unnecessary when decimals are greater: >  3.4567<
Seems unnecessary when decimals are greater: >      3.4567<
Seems unnecessary when decimals are greater: >0000003.4567<
云淡月浅 2024-11-26 03:30:02

可能只是猜测,但是:
精度为小数指定了一个长度,如果您有更多的小数,则不会超过该长度。
同样,宽度可以防止您的号码占用比应有的空间更少的空间。
如果您想到某种带有数字的表格,则只有当每行的每一列具有相同的宽度(无论其包含多少数字)时,您才能实现统一的列。

因此,在某些价格(例如 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文