How do I print a double value with full precision using cout in C++?

C++Server Side ProgrammingProgramming

The output stream cout allows using manipulators that you can use to set the precision directly on cout and use the fixed format specifier. To get the full precision of a double, you can use the limits library. For example,

Example

#include<iostream>
#include <limits>
using namespace std;
int main() {
    // Get numeric limits of double
    typedef std::numeric_limits< double > dbl;
    double PI = 3.14159265358979;
    cout.precision(dbl::max_digits10);
    cout << "Pi: " << fixed << PI << endl;
    return 0;
}

Output

This will give the output −

3.14159265358979
raja
Published on 28-Feb-2018 15:57:01
Advertisements