printf()函数中的width.precision

对于width来说,文档原文是:
Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.

也就是说,如果width大于数字的实际长度,会以空格的形式补齐缺少的位。如果width小于实际的数字长度,也不会对数字进行缩减来达到width的要求。

对于precision,文档原文是:

For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0.For a, A, e, E, f and F specifiers:
this is the number of digits to be printed after the decimal point (by default, this is 6)
. For g and G specifiers: This is the maximum number of significant digits to be printed. For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered. For c type: it has no effect. When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed.
也就是说,precision指定了小数点后面的位数,该位数默认是6位。

发布了267 篇原创文章 · 获赞 38 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/dghcs18/article/details/103998571