I am trying to print some floating point numbers using printf.
For example:
int main()
{
printf("%.1f",76.75);
return 0;
}
Output: 76.8
And I have some questions about the result.
First of all, why didn't it print 76.7?
Second, how did it round the number?
76.75is represented exactly.%.1fformatting string.37.975not convert to 37.98?”. This question is “Why does76.75convert to 76.8?”. They are symmetrically opposed questions. Besides, the answers there blame rounding in the decimal-to-floating-point conversion that happened during the compilation of37.975. The correct answer here should explain that this is the normal rounding behavior of floating-point-to-decimal conversion for a value that is exactly midway between two decimal representations.