You can use:
#include <iostream>
#include <limits>
using namespace std;
int main() {
double c = 123456789.12;
cout.precision(numeric_limits<double>::digits10 + 1);
cout << c << endl;
return 0;
}
Basically the limits
package has traits for all the build-in types.
One of the traits for floating point numbers (float/double/long double
) is the digits10
attribute. This defines the accuracy of a floating point number in base 10.
See it live: http://ideone.com/Ity9m7
To read on, check out another similar question: How do I print a double value with full precision using cout?