When using cout
, what is the default formatter defined in the <iomanip>
header? In other words, once I've set my formatter to fixed
using cout << fixed << setPrecision(2)
, how do I change it back? Or, what am I changing it back to?
The opposite of std::fixed
is std::scientific
.
(You find a nice list of manipulators in this great answer.)
The answer is std::defaultfloat
in C++11. To achieve this in C++03 you can do
cout.unsetf(std::ios_base::floatfield);
cout.unsetf(std::ios_base::floatfield);
this working with C++11 but not std::defaultfloat
–
Boxing cout.unsetf(std::ios_base::floatfield);
why it only reverse the effect of fixed
as it not include any keyword like fixed
means why it not reset precision
also –
Boxing The opposite of std::fixed
is std::scientific
.
(You find a nice list of manipulators in this great answer.)
You can use resetiosflags()
to unset any flags.
std::ios::scientific
. –
Hyposthenia iword
and pword
. (Of course, James was fully aware of the limitations.) You can, however, get pretty far with std::ios::flags()
. –
Hatchery ostringstream
and then just send the already formatted output to cout
as a std::string
. It may be more costly to do it this way (output is not usually a place where I spend a lot of time optimizing or investigating performance issues, as usually other hardware related concerns outweigh any performance decreases). –
Hyposthenia The opposite of std::fixed
is std::scientific
. That might do for you.
However, if you want to restore more flags, or if you need the previous state, instead of the default you can use better solutions:
the
std::resetiosflags
manipulator lets you reset specific flags to their defaults;the two
ios::flags
functions let you save and restore the previous values of the format flags.
std::fixed
is not std::scientific
. it is more like an automatic format-switching mode. –
Cathead © 2022 - 2024 — McMap. All rights reserved.