J
Johannes Bauer
Hello group,
I've a simple problem with the precision specifiers of stringstream.
Let's say I have this:
double x = 123.4567890;
std::stringstream Strm;
Strm.precision(4);
Strm << std::scientific << x;
std::cerr << Strm.str() << std::endl;
It will display "123.4". Now when I change x to be 1, it will display
"1". However, in that case I'd prefer if it could display "1.000", i.e.
not making the precision field the *minimum* precision but always the
exact displayed precision.
When I change to "fixed" I get the desired behavior - somehow, at least:
it only affects the digits after the decimal point, which is also what I
do not want. To sum it all up, here is what value yield which
representation and which I'd like:
Value scient fixed Preferred
1 1 1.0000 1.000
12 12 12.0000 12.00
123 123 123.0000 123.0
1.23 1.23 1.2300 1.230
1.234 1.234 1.2340 1.234
12345 1.234E4 12345.0000 1.234E4
10000 1E4 10000.0000 1.000E4
I don't know if this is possible, but I'm happy to hear any suggestions.
Thanks in advance,
Johannes
I've a simple problem with the precision specifiers of stringstream.
Let's say I have this:
double x = 123.4567890;
std::stringstream Strm;
Strm.precision(4);
Strm << std::scientific << x;
std::cerr << Strm.str() << std::endl;
It will display "123.4". Now when I change x to be 1, it will display
"1". However, in that case I'd prefer if it could display "1.000", i.e.
not making the precision field the *minimum* precision but always the
exact displayed precision.
When I change to "fixed" I get the desired behavior - somehow, at least:
it only affects the digits after the decimal point, which is also what I
do not want. To sum it all up, here is what value yield which
representation and which I'd like:
Value scient fixed Preferred
1 1 1.0000 1.000
12 12 12.0000 12.00
123 123 123.0000 123.0
1.23 1.23 1.2300 1.230
1.234 1.234 1.2340 1.234
12345 1.234E4 12345.0000 1.234E4
10000 1E4 10000.0000 1.000E4
I don't know if this is possible, but I'm happy to hear any suggestions.
Thanks in advance,
Johannes