It seems you want to get the "best" formatting (which is the default for std::ostream
): you can use %g
(%Lg
for long double
) to have the formatting function decide how the values should be formatted. There are four format specifiers:
%f
for fixed point notation (the format used bystd::fixed
for streams).%e
for scientific formatting (the format used bystd::scientific
for streams).%g
for the "best" version of fixed and scientific (the default for streams and since C++11std::defaultfloat
).%a
for an exact/hex representation of the floating point number (since C++11std::hexfloat
).
The formatting flags can used both in lowercase and uppercase to indicate whether any characters should be lowercase or uppercase (e.g. e
vs. E
). Note that the l
length specifier is actually not relevant for the floating point formatting. You might need to use L
, though, when formatting long double