What is the difference between cout.precision() and setprecision()?

I can use both to do the same thing in my code, so is there any benefit of using one or the other?


double myvariable(4.2432);

cout.precision(2);
cout << myvariable << " using cout.precision \n";

cout << setprecision(2) << myvariable << " using setprecision \n";

Both output 4.2
Last edited on
See this:

http://www.cplusplus.com/reference/iomanip/setprecision/

The advantage of precision(...) is that it returns the previous value, so that you are able to undo the change.
Topic archived. No new replies allowed.