开发者

have a nice representation of a string from a double

开发者 https://www.devze.com 2023-03-09 00:00 出处:网络
I have a double 1.4291400000000001 I want to have a string representation of that double. What is the best/standard way to get 1.4开发者_运维百科2914What about:

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

What is the best/standard way to get 1.4开发者_运维百科2914


What about:

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


It's not guaranteed to have a double to have exactly this value : 1.4291400000000001. It could be a little greater than that or little smaller than that. Much less like to be exactly that.

After this fact, I can say that there is no best way to get 1.42914 from that. You might get 1.42913 instead, as the actual double value might be : 1.4291399999999999 instead of the above value.

Read this: What Every Computer Scientist Should Know About Floating-Point Arithmetic

0

精彩评论

暂无评论...
验证码 换一张
取 消