Reputation: 35
I am trying to print a float to no decimal places and I used %0.f and %.f and they gave me the same result. Is there a difference between them?
Upvotes: 3
Views: 4917
Reputation: 70909
Yes, they are the same.
The number to the left of the decimal is the minimum number of digits to print before padding with white space.
If that number begins with a zero, it also indicates that the number should be padded with zeros instead of spaces. For a length of zero, no padding will be applied. It's impossible to print a number of length less than zero, so the additional padding indicator will never be used.
In either case, one must print some number, which means that even though you indicated you want a minimally zero length float, it will print more than the minimum length to show the number's value. For your test cases, the output of printf("%0.f", 0.0f)
will show 0
, having decided to print more than the minimum number of characters to show the number.
For those confusing %0.f
with %.0f
note that one specifies minimum number of digits, and the other is a precision modifier.
For floats, the presence of the .
character indicates a specified precision, and the absence of a number to the right is interpreted as 0
. So %3.0f
and %3.f
are equivalent. In the event you do not supply a .
in your float, then it is assumed that your float is effectively formatted with .6
Upvotes: 3
Reputation: 49
From the C99 Ansi Standard section 7.19.6.10 regarding fprintf of floats:
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.
So the result will probably be the same whether you write '%.f' or '%0.f' if your number needs a decimal, and it will not cut it off as might be expected.
Upvotes: 0
Reputation: 90995
printf
format codes of the form %M.Nf
, where M and N are integers, mean to round the number to N decimal places, and left-pad it with spaces until it takes up at least M screen columns.
>>> printf('%10.2f', 12345.6789);
12345.68
^^ Note the 2 spaces here to produce 10 chars of output.
Key word here being “at least”. If the number “naturally” takes up more than M columns, then it won't be truncated.
%0.f
(with the 0
on the left side of the .
) literally means to pad the number with spaces until it's at least 0 columns wide. But since there's no such thing as a negative-width numeric string, it could never actually write any space-padding, so it's equivalent to just %.f
.
Upvotes: 0
Reputation: 84
No. They’re not the same. The output will look the same though if the float doesn’t have any digits after the decimal point. When there are digits after the decimal, depending upon the compiler %.0f will either truncate, round, floor or ceil to remove them. %.f displays whatever digits are provided up to a compiler specified maximum. Typically ~6 digits. It’s usually set to something sensible based on the size of float and the corresponding number of digits.
Upvotes: 0