Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between %0.2lf and %.2lf as printf placeholders?

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.

But what does putting a single 0 before 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.

If it does nothing, is there a preferred format?

like image 974
Marcus McLean Avatar asked Apr 02 '13 13:04

Marcus McLean


People also ask

What is the meaning of 0.2 F in C?

%0.2f floating point at least 0 wide and 2 number after decimal. %.2f floating point at least 0(default) wide and a precision of 2) But don't misunderstand about the 0 width if you use %0.2f it can auto adjust its minimum width.

What is the difference between F and 2f in C?

“print” treats the % as a special character you need to add, so it can know, that when you type “f”, the number (result) that will be printed will be a floating point type, and the “. 2” tells your “print” to print only the first 2 digits after the point.

What is the meaning of .2f in C?

2f" tells the printf method to print a floating point value (the double, x, in this case) with 2 decimal places. Similarly, had we used "%. 3f", x would have been printed rounded to 3 decimal places.


1 Answers


%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)

like image 50
a3.14_Infinity Avatar answered Nov 15 '22 21:11

a3.14_Infinity