C ++ equivalent std :: setprecision (20) using printf in C

I want to print double numbers in decimal notation with full precision (but without extra zeros at the end of the number). In C ++, I can use:

std::setprecision(20); cout << d; // Here d is a double 

What would be the equivalent C code with printf?

+4
source share
3 answers

You can use the qualifier "% .20g". g IMHO is usually better than f, since it does not print trailing zeros and intelligently processes large / small values ​​(changes to e format).

Also note that using the "g" qualifier, precision ("20" in this case) determines the number of significant digits, not the number of digits after the decimal point.

+9
source

The format is usually: %[flags][width][.precision][length]specifier , for example, %.20f

If you pass .* .20 than .20 , you can pass arbitrary precision at run time to a decimal value.

NOTE: you can use g too, however you should notice that in some cases there will be a difference in the result (between f and g - because of how the accuracy is interpreted.)

The main question, although why do you need such accuracy? (double / float are inaccurate) ...

+7
source

Using:

 printf("%.20f", d); 

That should work.

Adapted from an online document , more general format:

 %[flags][width][.precision][length]specifier 

Follow the link to read about each token in a format.

+4
source

All Articles