Is there a simple, quick way in C to convert or print a float so that the output has a precision of two decimals (not more), but in case there are any trailing zeroes they get cut out?
Examples:
100.456 -> 100.46
100.32 -> 100.32
100.7046 -> 100.7
100.00 -> 100
Let's say we have float x;
and we execute printf(%.2f, x)
.
This would satisfy the first requirement, since any float would be printed with a maximum precision of 2 decimals. The problem though is that for x=100.00
or 100.7046
we would get respectively 100.00
and 100.70
.
Using "g" as a format wouldn't fix the problem either. Any ideas?