简体   繁体   English

double.parse 返回精度差异

[英]double.parse returning a precision difference

Why does double.parse ("1833.35") return value 1833.349999999999 ?为什么double.parse ("1833.35")返回值1833.349999999999

It's because floating-point values are represented in the machine with binary exponents, and 10 is not an even power of 2. Thus, some base-10 fractions turn into repeating fractions when converted.这是因为浮点值在机器中用二进制指数表示,而 10 不是 2 的偶数幂。因此,一些以 10 为底的分数在转换时会变成重复分数。

To illustrate, just work out how you'd represent 35/100 in binary.为了说明,只需计算出如何用二进制表示 35/100。

You could switch to using a decimal instead of a double if you need it to be absolutely correct.如果您需要绝对正确,您可以切换到使用小数而不是双精度。 Decimal stores the number with a base 10 exponent. Decimal 存储以 10 为底的指数的数字。

There is no way to represent.35 exactly using a double or float.没有办法完全使用双精度或浮点数来表示。35。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM