如何显示浮点数的附加数字?
我有一个像这样创建的 Double :
Double d = Double.parseDouble( "27.86753" ); // All the digits of this double value are 27.867530822753906
这个特殊的双精度数也可以用浮点数表示,因此 Java 会丢弃其余的数字。如何强制 Java 给出该值的真正双精度表示形式(所有数字)?
I have a Double which I created like this:
Double d = Double.parseDouble( "27.86753" ); // All the digits of this double value are 27.867530822753906
This particular double can also be represented by a float, so Java drops the rest of my digits. How can I force Java to give me the true double representation (all of the digits) of this value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
哦,伙计,有趣。您想确切地了解
double
如何不准确地执行此操作。我认为
new BigDecimal(double).toString()
会给您所有不正确的结果数字,因为BigDecimal
表示具有精确指定精度的数字。编辑:哦哦哦哦,我想我明白发生了什么事。
让我尝试这样解释它:
Double.toString
返回最不精确的String
,这样Double.parseDouble
将返回完全相同的>double
-- 刚好足够的数字来“唯一标识”确切的 IEEE 754 值,尽管实际值可能比打印的数字更多。仅仅因为Double.toString
没有提供您期望的位数并不意味着实际 IEEE 754 双精度值会四舍五入到那么多位数。new BigDecimal(double).toString
将返回所存储的准确 IEEE-754 值。更新:
发生的事情是,当它打印出
27.86753
时,它实际上是一个比您引用为正确答案的数字27.867530822753906更精确的值
。这是因为toString
被设计为仅打印必要的内容,以确保Double.parseDouble(Double.toString(value))
是无操作。我运行了以下代码:
}
并打印出来
事实上,第二个值明显更接近 27.86753,差距约为 0.0000008。
Oh, man, interesting. You want to learn exactly how
double
does it inaccurately.I think
new BigDecimal(double).toString()
will give you all of the incorrect digits, sinceBigDecimal
represents a number with a precisely specified precision.EDIT: Ohhhhhhhh, I think I see what's going on.
Let me try to explain it like this:
Double.toString
returns the least preciseString
such thatDouble.parseDouble
will return exactly the samedouble
-- just enough digits to "uniquely identify" the exact IEEE 754 value, though the actual value may have more digits than are printed. Just becauseDouble.toString
isn't giving you as many digits as you expect doesn't mean that the actual IEEE 754 double-precision value is being rounded to that many digits.new BigDecimal(double).toString
will return the exact IEEE-754 value being stored.UPDATE:
What's going on is that when it prints out
27.86753
, it's actually internally a more precise value than the digits you're quoting as the correct answer,27.867530822753906
. That's just becausetoString
is designed to print out only as much as necessary to ensure thatDouble.parseDouble(Double.toString(value))
is a no-op.I ran the following code:
}
and it printed out
And indeed, this second value is noticeably closer to 27.86753, by a margin of something like 0.0000008.
您可能需要考虑使用
java.math.BigDecimal
,您可以通过明确的方法来控制比例和舍入规则。你不能用 Double 做到这一点。You might want to consider using
java.math.BigDecimal
where you have explicit ways to control scale and rounding rules. You cannot do it with Double.丑陋但有效:
Ugly but it works: