C语言 %0.2lf 和 %.2lf 作为 printf 占位符有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15765289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between %0.2lf and %.2lf as printf placeholders?
提问by Marcus McLean
I am aware that putting any number of 0's before the width of the placeholder implements zero-padding. For example, printf("%02d", 6);prints 06.
我知道0在占位符的宽度之前放置任意数量的's 实现零填充。例如,printf("%02d", 6);打印06.
But what does putting a single 0before the precision of the placeholder do? For example, for both printf("%0.2lf", 0.123);and printf("%.2lf", 0.123);, the output is 0.12.
但是0在占位符的精度之前放一个单曲有什么作用呢?例如,对于printf("%0.2lf", 0.123);和printf("%.2lf", 0.123);,输出都是0.12。
If it does nothing, is there a preferred format?
如果什么都不做,是否有首选格式?
采纳答案by Mats Petersson
They are "equivalent". If you were to use "%07.2", then it would make a difference, by adding extra zeros on the front.
它们是“等价的”。如果您要使用“%07.2”,那么通过在前面添加额外的零会有所不同。
Edit: Originally had "%04.2", which of course doesn't make any difference, because a float with two decimals is always 4 wide anyway.
编辑:最初有“%04.2”,这当然没有任何区别,因为无论如何,带有两位小数的浮点数总是4宽。
回答by a3.14_Infinity
%3.2f //(print as a floating point at least 3 wide and a precision of 2)%0.2lf //(print as a floating point at least 0 wide and a precision of 2)%.2lf //(print as a floating point at least 0(default) wide and a precision of 2)
%3.2f //(print as a floating point at least 3 wide and a precision of 2)%0.2lf //(print as a floating point at least 0 wide and a precision of 2)%.2lf //(print as a floating point at least 0(default) wide and a precision of 2)
回答by teppic
These examples should show the difference:
这些示例应该显示出不同之处:
"%0.2lf", 0.123-> 0.12(zero padded min. width of 0, 2 decimal places).
"%0.2lf", 0.123-> 0.12(零填充最小宽度为 0,小数点后 2 位)。
"%6.2lf", 0.123-> __0.12(space padded min. width of 6, 2 decimal places).
"%6.2lf", 0.123-> __0.12(空间填充最小宽度为 6,小数点后 2 位)。
"%06.2lf", 0.123-> 000.12(zero padded min. width of 6, 2 decimal places).
"%06.2lf", 0.123-> 000.12(零填充最小宽度为 6,小数点后 2 位)。
"%0.6lf", 0.123-> 0.123000(min width of 0, 6 decimal places).
"%0.6lf", 0.123-> 0.123000(最小宽度为 0,小数点后 6 位)。
The first zero specifies zero padding, followed by the minimum width, which has a default of 0. Thus it is effectively ignored by itself (since you cannot pad 0 width).
第一个零指定零填充,然后是最小宽度,默认值为 0。因此它本身被有效地忽略(因为你不能填充 0 宽度)。
顺便说一句,正确的形式
%f%f,而不是%lf%lf对printfprintf。回答by Aatish Sai
Blockquote
块引用
Basically when we % w.p ffor output wrefers to the minimum number of position to be use for displaying the value and prefers to the number of digit after decimal point.
%3.2ffloating point having 3 wide and 2 number after decimal
基本上,当我们% w.p f输出时,w指的是用于显示值的最小位置数,p指的是小数点后的位数。
%3.2f小数点后有 3 个宽和 2 个数字的浮点数
%0.2ffloating point at least 0 wide and 2 number after decimal
%0.2f浮点数至少为 0 位宽和 2 位小数点后的数字
%.2ffloating point at least 0(default) wide and a precision of 2)
%.2f浮点数至少为 0(默认)宽,精度为 2)
But don't misunderstand about the 0 width if you use %0.2fit can auto adjust its minimum width.
但是不要误解 0 宽度,如果您使用%0.2f它可以自动调整其最小宽度。

