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?
%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.
“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.
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.
%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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With