Isnt setprecision not supposed to change the value stored in variable?
<p>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.</p>

<p>Consider the following code snippet:</p>

<pre><code>int main()
{
    double x = 9.87654321;
    cout &lt;&lt; setprecision(3) &lt;&lt; fixed &lt;&lt; x &lt;&lt;endl;    //Returns 9.877 as it should
    cout &lt;&lt; x &lt;&lt; endl;                               //Returns truncated value 9.877 again though it shouldnt.

    return 0;
}   
</code></pre>

<p>Interesting part is, if we replace <code>cout &lt;&lt; x &lt;&lt; endl;</code> by a line setting precision to say 7, then it DOES display the correct value. Can anyone please explain this phenomenon?</p>
