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

Recommended Answers

All 4 Replies

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?

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();
}

The 5 digits before have nothing to do with it. When your system is rounding to tenths then xxxx.47.. is rounded to xxxx.5 just as xxxxxx.79.. is rounded to xxxxxx+1.
Read also : http://en.wikipedia.org/wiki/Rounding

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.