Home > Article > Backend Development > How many decimal places does the double type in C language output by default?
There are two types of decimals commonly used in C language, namely float or double; float is called single-precision floating point type, and double is called double-precision floating point type. Unlike integers, decimals don't have so many problems. The length of decimals is fixed. Float always occupies 4 bytes, and double always occupies 8 bytes.
How many decimal places does the double type in c language output by default?
In C language, when outputting double type (double precision real type) and float type (single precision real type), 6 decimal digits are output by default (less than six digits are filled with 0, and more than six digits are padded with 0s) Truncated by rounding).
double a = 1; printf("%lf\n", a);
The output will be:
1.000000
But sometimes six digits will seem very long and unnecessary. For example, when calculating the average score, one to two decimal places is enough. But sometimes six digits are not enough and more decimal places are needed, such as calculating high-precision square roots. At this time, you can use printf format control. If you want to output n decimal places, you can use the %.nlf format. where n is a number.
If you want to output 10 decimal places, then
printf("%.10lf\n", a);
is enough.
Recommended: "c Language Tutorial"
The above is the detailed content of How many decimal places does the double type in C language output by default?. For more information, please follow other related articles on the PHP Chinese website!