0

When i use a variable type double the value e.g 345624.769123 rounded to 345625
but when i take 5 digits before decimal i.e 34562.4769123 then it shows 34562.5

Please explain Why?

Thanks

3
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by ArkM
0

When i use a variable type double the value e.g 345624.769123 rounded to 345625
but when i take 5 digits before decimal i.e 34562.4769123 then it shows 34562.5
Please explain Why?
Thanks

Please explain where and when?

0

Please explain where and when?

#include<iostream>
#include <conio.h>

using std::cout;
using std::cin;

main()
{
double x=345624.769123;
cout<<"x=";
cout << x;
getch();
}

0

It's simply default precision of stream output. Don't worry, double type always keeps ~16 significant digits...
Use setprecision manipulator from <iomanip> to set another output precision:

#include <iomanip>
double x = ......;
...
for (int n = 0; n < 17; ++n)
    cout << setprecision(n) << x << endl;

Try this and see what happens...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.