gpt4 book ai didi

c - 为什么使用 float 时会打印这种格式化输出?

转载 作者:行者123 更新时间:2023-11-30 20:03:33 26 4
gpt4 key购买 nike

在此基础代码中:

   float j=9.01;
printf("%2.3f \n",j);

输出是9.010,它不应该像2个字符宽一样为整个j保留,其中包括3 为小数 - 所以它是数字。小数点之前,即 9 消失,只打印 01

我哪里出错了?

最佳答案

让我们quote the man page :

Field Width

In no case does a nonexistent or small field width cause truncation of a field; if the result of a conversion is wider than the field width, the field is expanded to contain the conversion result.

The precision

This gives the minimum number of digits to appear for d, i, o, u, x, and X conversions, the number of digits to appear after the radix character for a, A, e, E, f, and F conversions [...]

字段宽度太小,但字段永远不会被截断,所以这并不重要。

句号后面的部分显然是精度,而你要求三位数。

关于c - 为什么使用 float 时会打印这种格式化输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49431658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com