C Decimal Precision
Set Decimal Precision
You have probably already noticed that if you print a floating point number, the output will show many digits after the decimal point:
Example
float myFloatNum = 3.5;
double myDoubleNum = 19.99;
printf("%f\n", myFloatNum);
// Outputs 3.500000
printf("%lf", myDoubleNum); // Outputs
19.990000
Try it Yourself »
If you want to remove the extra zeros (set decimal precision), you can use a
dot (.) followed by a number that specifies how many digits that should be shown
after the decimal point:
Example
float myFloatNum = 3.5;
printf("%f\n", myFloatNum); // Default will show 6 digits after the decimal point
printf("%.1f\n",
myFloatNum); // Only show 1 digit
printf("%.2f\n", myFloatNum); // Only
show 2 digits
printf("%.4f", myFloatNum); // Only show 4 digits
Try it Yourself »
Rounding
When you set the precision, printf() does not just cut off the extra digits. It rounds the number to the nearest value:
Example
float myNum = 3.14159;
printf("%.2f\n", myNum); // 3.14
printf("%.3f\n", myNum); // 3.142 (rounded up)
printf("%.0f", myNum); // 3
Try it Yourself »
Visualize Decimal Precision
Choose a format specifier below to see how 3.14159 is printed:
Code
Output
Explanation