0E30 不为零
我不知道如何处理new BigDecimal("0E30")
。它的值为 0
,但它不与 BigDecimal.ZERO
进行比较。请参阅下文:
System.out.println(new BigDecimal("0E30").add(BigDecimal.ONE)); // ---> 1
System.out.println(new BigDecimal("0E30").equals(BigDecimal.ZERO)); // ---> false
有人可以帮助我使比较正确吗(我知道我可以通过将 BigDecimal
转换为 double
来获得解决方法,但我想知道什么是正在进行)? 我正在使用 JRE 1.6.3。 谢谢
I dont know how to handle new BigDecimal("0E30")
. Its value is 0
but it doesn't compare to BigDecimal.ZERO
. See below:
System.out.println(new BigDecimal("0E30").add(BigDecimal.ONE)); // ---> 1
System.out.println(new BigDecimal("0E30").equals(BigDecimal.ZERO)); // ---> false
Could someone help me to make the comparison true (I know I can get a workaround by converting the BigDecimal
s to double
, but I would like to know what is going on)?
I am using JRE 1.6.3.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 docs(重点是我的):
在本例中,比例不匹配。因此,您应该使用compareTo()。
BigDecimal
是equals()
与compareTo()
不一致的情况之一:From the docs (emphasis is mine):
In this case, the scale doesn't match. So instead, you should use
compareTo()
.BigDecimal
is one of the cases whereequals()
is inconsistent withcompareTo()
:来自文档:
您的比例是不同的。如果您希望它们比较相等,请使用compareTo。
From the docs:
Your scales are different. If you want them to compare equal, use
compareTo
.