Have a good view of the string from double

I have a double 1.4291400000000001 I want to have a string representation of this double.

What is the best / standard way to get 1.42914

+3
source share
2 answers

What about:

std::stringstream ss;
ss << std::fixed << std::setprecision( N ) << double_number;
// ss.str() gives you the string 
+10
source

It is not guaranteed that doublehas this value: 1.4291400000000001. It may be a little more or less than that. Much less like being that.

After that, I can say that there is no better way to get 1.42914from this. Instead, you can get 1.42913, since the actual double value could be: 1.4291399999999999instead of the value above.

:

+2

All Articles