I was of the opinion that setprecision doesnt change the value in variable itself. Also, when you attach setprecision to cout, it sticks with it only once. However, when I run code to verify, it doesnt work.
Consider the following code snippet:
int main()
{
double x = 9.87654321;
cout << setprecision(3) << fixed << x <<endl; //Returns 9.877 as it should
cout << x << endl; //Returns truncated value 9.877 again though it shouldnt.
return 0;
}
Interesting part is, if we replace cout << x << endl;
by a line setting precision to say 7, then it DOES display the correct value. Can anyone please explain this phenomenon?