You may have observed that when printing a floating-point number, the output displays numerous digits after the decimal point.
float myFloatNum = 3.5; double myDoubleNum = 19.99; printf(“%f\n”, myFloatNum); // Outputs 3.500000 |
To eliminate trailing zeros and set decimal precision when printing, you can use a dot ( . ) followed by a number indicating how many digits should appear after the decimal point.
float myFloatNum = 3.5;
printf(“%f\n”, myFloatNum); // Default will show 6 digits after the decimal point |