睿宏 陳
PRO
last year
睿宏country asked

In C, I assigned 78.32 to a float, but the output shows 78.320000. Why?

Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi 睿宏,

That happens because printf("%f", ...) shows 6 digits after the decimal by default, so 78.32 becomes 78.320000.

If you want only 2 decimal places, set the precision like this:

printf("%.2f", number);

If you have more questions, I am here to help 😊

C
This question was asked as part of the Learn C Programming course.