1

Possible Duplicate:
floating point precision

when I do cout<<8.0 .Its getting printed as 8.How to Print in the output console of c++ the entire zeros after decimal point like 8.00000000 I tried this cout<<setprecision(5)<<(double)8.0; still printing 8

Community
  • 1
  • 1
nightrider
  • 95
  • 8

2 Answers2

2

Use the fixed manipulator

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    cout << fixed << setprecision(6) << (double)8 << "\n";
    return 0;
}

http://ideone.com/ShcNIc

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • Thanks Problem solved.Juz one more thing is there a way to determine the number of values after decimal.Because setprecision also takes in account the mantissa – nightrider Dec 06 '12 at 02:54
  • Actually it doesn't, if I understand your question correctly. See http://www.cplusplus.com/reference/iomanip/setprecision/ -- it says "In both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if this includes trailing decimal zeros. The number of digits before the decimal point does not matter in this case." – Ray Toal Dec 06 '12 at 04:17
0

See How do I print a double value with full precision using cout?

cout.precision(15);
cout << fixed << 8.0;
Community
  • 1
  • 1