0
main()
{
   double d1 = 1234.1;
   cout << "d1 = 1234.1 --> " << d1 << endl;
   double d2 = 1234.099999;
   cout << "d2 = 1234.099999 --> " << d2 << endl;
}

Output:

d1 = 1234.1 --> 1234.1
d2 = 1234.099999 --> 1234.1

How can I get the exact value for d2 ? Please suggest.

user
  • 6,897
  • 8
  • 43
  • 79
  • 2
    Aside from output rounding, keep in mind that `double` is not an exact numeric type to begin with. – user Mar 04 '11 at 08:46
  • 2
    Please don't post the exact same question twice: [How to maintain Double's precision in C++](http://stackoverflow.com/questions/5191018/how-to-maintain-doubles-precision-in-c) – Cody Gray - on strike Mar 04 '11 at 09:28

1 Answers1

0

Try

cout.precision(<number of digits after comma>);

like

cout.precision(5);

before output statement.

maverik
  • 5,508
  • 3
  • 35
  • 55