Open In App

How Do I Print a Double Value with Full Precision Using cout?

Last Updated : 25 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The double value in C++ has a precision of up to 15 digits but while printing it using cout, it only prints six significant digits. In this article, we will learn how to print the double value with full precision.

For Example,

Input:
double var = 12.3456789101112

Output:
var = 12.3456789101112

Print a Double Value with Full Precision in C++

To print a double value with full precision using cout in C++, we can use the std::fixed to set the floating-point representation to fixed-point notation and std::setprecision to set the precision to the maximum possible digits for a double using std::numeric_limits<double>::max_digits10.

std::fixed and std::setprecision are defined inside <iomanip> header and std::numeric_limits<double>::max_digits10 is defined inside <limits> header so we need to include them in our program.

C++ Program to Print the Double Value with Full Precision

C++




// C++ program to print the double value with maximum
// precision using cout
#include <iomanip>
#include <iostream>
#include <limits>
using namespace std;
  
int main()
{
    // double with 15 digits
    double myDouble = 123.456789012345;
  
    // setting precision to max precision for double
    cout << fixed
         << setprecision(
                numeric_limits<double>::max_digits10)
         << myDouble << endl;
  
    return 0;
}


Output

123.45678901234499847

The above method will also work if the size of the double changes due to different CPU architecture. If your program only runs in a single architecture such as 64-bit CPU, then use can simply set the precision to 15 digits.

C++ Program to Print Double Value with Full Precision Using Absolute Precision

C++




// C++ program to print the double value with maximum
// precision using cout
#include <iomanip>
#include <iostream>
#include <limits>
using namespace std;
  
int main()
{
    // double with 15 digits
    double myDouble = 123.456789012345;
  
    // setting precision to max precision for double
    cout << setprecision(15) << myDouble << endl;
  
    return 0;
}


Output

123.456789012345

Note: The precision of the double value is only upto 15 digits. If your value has more than 15 digits, the last value will be rounded off and the rest of the values may not be accurate.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads