How many decimal places does the double type in C language output by default?

青灯夜游
Release: 2020-04-22 15:25:05
Original
6780 people have browsed it

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);
Copy after login

The output will be:

1.000000
Copy after login

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!