I want to print doubles 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 using printf?
The C++ setprecision function is used to format floating-point values. This is an inbuilt function and can be used by importing the iomanip library in a program.
c++ - std::setprecision sets the number of significant figures.
The cout object is the only print method specifically created for C++. cout is an object of the ofstream type. C++ was designed around object-oriented programming and has completely different syntax compared to the functions deriving from C. Accordingly, cout is considered the standard way of printing strings in C++.
You can use the "%.20g" specifier. g is IMHO usually better than f since it doesn't print trailing zeros, and handles large/small values sensibly (changing to e format).
Also note that with the "g" specifier, the precision (the "20" in this case) specifies the number of significant digits rather than the number of digits after the decimal point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With