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?
Asked
Active
Viewed 1.4k times
4 Answers
16
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` – Abhishek Mane Jun 14 '21 at 15:03
-
and also `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 – Abhishek Mane Jun 14 '21 at 16:24
5
The opposite of std::fixed
is std::scientific
.
(You find a nice list of manipulators in this great answer.)
-
2The default float and the scientific notation are different. It is not exactly "changing it back". – SOFe Jun 21 '19 at 11:25
1
You can use resetiosflags()
to unset any flags.

Chad
- 18,706
- 4
- 46
- 63
-
-
-
@Moshe: Unfortunately, there is no simple way to fully reset a stream. Even the most elaborate code I have seen to do that (by James Kanze, more than a decade ago) misses on some esoteric propertiesm, like `iword` and `pword`. (Of course, James was fully aware of the limitations.) You can, however, get pretty far with [`std::ios::flags()`](http://www.cplusplus.com/reference/iostream/ios_base/flags/). – sbi Sep 14 '11 at 20:04
-
1The easiest way to reset a stream is to default construct it. In some cases it may make sense to format your output in an `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). – Chad Sep 14 '11 at 20:13
1
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.

R. Martinho Fernandes
- 228,013
- 71
- 433
- 510
-
1uhm, in the sense of "go back to default" the opposite of `std::fixed` is not `std::scientific`. it is more like an automatic format-switching mode. – Cheers and hth. - Alf Sep 14 '11 at 20:12