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

Time:2021-1-28

There are two types of decimal commonly used in C language: 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 units. The length of decimals is fixed. Float always takes up 4 bytes and double always takes up 8 bytes.

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

In C language, when double type (double precision real type) and float type (single precision real type) are output, 6 decimal places are output by default (less than 6 decimal places are filled with 0, more than 6 decimal places are rounded off).


double a = 1;
printf("%lf\n", a);

The output will be:

1.000000

But sometimes the six will be long and unnecessary. For example, one or two decimal places is enough to calculate the average score. But sometimes six digits is not enough, and more decimals are needed, such as calculating the square root of high precision. In this case, you can use the format control of printf. If you want to output N decimal places, you can use%. NLF format. Where n is a number.

If you want to output 10 decimal places, then


printf("%.10lf\n", a);

That’s it.

Content expansion

Decimal output

Decimal can also be output by using printf function, including decimal form and exponential form. Their corresponding format controllers are as follows:

  • %F outputs the float type in decimal form;
  • %LF outputs double type in decimal form;
  • %E outputs the float type in exponential form, and the E in the output result is lowercase;
  • %E outputs the float type in exponential form, and the E in the output result is capitalized;
  • %The double type is output in exponential form, and the E in the output result is lowercase;
  • %Le outputs the double type in exponential form, and the E in the output result is capitalized.

The following code demonstrates the decimal representation and output:


#include <stdio.h>
#include <stdlib.h>
int main()
{
  float a = 0.302;
  float b = 128.101;
  double c = 123;
  float d = 112.64E3;
  double e = 0.7623e-2;
  float f = 1.23002398;
  printf("a=%e \nb=%f \nc=%lf \nd=%lE \ne=%lf \nf=%f\n", a, b, c, d, e, f);
  
  return 0;
}

Results of operation:

a=3.020000e-01
b=128.100998
c=123.000000
d=1.126400E+05
e=0.007623
f=1.230024

So far, this article about how many decimal places are output by default of double type in C language is introduced here. For more information about decimal output of double type in C language, please search previous articles of developer or continue to browse the following related articles. I hope you can support developer more in the future!

Recommended Today

Don’t be a tool man. Touching hands teaches you Jenkins!

Hello everyone, I’m a piece of cake, a piece of cake eager to be Cai Bucai in the Internet industry. Soft or hard, praise is soft, white whoring is just!Ghost ~ remember to give me a third company after watching it! This article mainly introducesJenkins If necessary, please refer to If it helps, don’t forgetgive […]