我在 Android Java 中有以下表达式:
myVar + " == 0 ? " + BigDecimal.ZERO.equals(myVar)
输出如下:
0.0000 == 0 ? false
myVar
声明为:
public BigDecimal myVar;
并成功分配Gson
,来自提供的 JSON 数据(我对此进行了检查以验证 JSON 是否良好)。
为什么 0.0000
作为 BigDecimal
不等于 BigDecimal.ZERO
?
最佳答案
来自 the documentation :
Returns true if x is a BigDecimal instance and if this instance is equal to this big decimal. Two big decimals are equal if their unscaled value and their scale is equal. For example, 1.0 (10*10-1) is not equal to 1.00 (100*10-2). Similarly, zero instances are not equal if their scale differs.
他们为什么这样决定?可能是因为它更容易实现。这是一个好的决定吗?我不这么认为,但事实就是如此。如果您不喜欢,请使用其他库。
关于java - 为什么我的 BigDecimal 0.0000 不等于 BigDecimal.ZERO?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31688290/