I want to print a double value to std::cout
portably (GCC, clang, MSVC++) such that the output is the same on all platforms.
I have a problem with the forma
There is no manipulator controlling the formatting of the exponent (I assume you mean the exponent rather than the mantissa; also, the "official" name used for the mantissa is significant). To make matters worse, I can't see any rule in the C standard which restricts the formatting of the exponent. I realize that this is about C++ but for the purpose of the formatting details, the C++ standard refers to the C standard.
The only approach I'm aware of is to use an own std::num_put
facet which formats the values as desired. This facet would then be put into a std::locale
which in turn is imbue()
ed into std::cout
. A potential implementation could use the default std::num_put
facet (or snprintf()
which is, unfortunately, probably simpler) to format the floating point number and then strip leading zeros from the exponent.