简体   繁体   English

BigDecimal if语句不适用于! 运营商?

[英]BigDecimal if statement does not work with ! operator?

I have two BigDecimals , value1 and value2 . 我有两个BigDecimalsvalue1value2 I want to divide value1 by value2 but obviously I don't want to do this if one of them is zero. 我想将value1除以value2但如果其中之一为零,显然我不想这样做。 So I wrote this simple if statement: 所以我写了这个简单的if语句:

if(!value1.equals(0) && !value2.equals(0)){
  divide value1 by value2 blah blah blah
}

For some reason this if statement did not work.. even when one of the values equaled zero the equation would still be evaluated giving me an error because I was trying to divide with or by zero. 由于某种原因,这个if语句不起作用。即使当其中一个值等于零时,该方程式仍会被求值,这给了我一个错误,因为我试图用零除或零。 I know that I can just use value1.compareTo(BigDecimal.valueOf(0) == 1 and get what I want doing that but I'm just curious as to why my first method didn't work. 我知道我可以只使用value1.compareTo(BigDecimal.valueOf(0) == 1并得到我想要的东西,但我只是好奇为什么我的第一种方法不起作用。

Java treats objects of different types as different. Java将不同类型的对象视为不同的对象。 This means that even if they represent something equivilent they are not the same. 这意味着即使它们表示等同物,它们也不相同。

What is "special" about BigDecimal is that even if the type is the same and the values are equivelent, but have different scales, they are still not "equals" BigDecimal的“特殊”之处在于,即使类型相同且值相等,但具有不同的小数位数,它们仍然不是“等于”

Double d = 0.0;
Integer i = 0;
System.out.println(d + " equals " + i + " is " + (d.equals(i)));

BigDecimal bd2 = new BigDecimal("0.00");
BigDecimal bd3 = new BigDecimal("0.000");

System.out.println(bd2 + " equals " + d + " is " + (bd2.equals(d)));
System.out.println(bd2 + " equals " + bd2 + " is " + (bd2.equals(bd2)));
System.out.println(bd2 + " equals " + bd3 + " is " + (bd2.equals(bd3)));

prints 版画

0.0 equals 0 is false
0.00 equals 0.0 is false
0.00 equals 0.00 is true
0.00 equals 0.000 is false

The way around this is to use compareTo which adjusts for the scale and gives you a more natural comparison. 解决此问题的方法是使用compareTo来调整比例并为您提供更自然的比较。

System.out.println(bd2 + " compareTo " + BigDecimal.ZERO + " is " + (bd2.compareTo(BigDecimal.ZERO )));
System.out.println(bd3 + " compareTo " + BigDecimal.ZERO + " is " + (bd3.compareTo(BigDecimal.ZERO )));

prints 版画

0.00 compareTo 0 is 0
0.000 compareTo 0 is 0

This means if you are going to use BigDecimal you need to write 这意味着如果您要使用BigDecimal,则需要编写

if(value1.compareTo(BigDecimal.ZERO) != 0 && value2.compareTo(BigDecimal.ZERO) != 0) {

Personally, I prefer to use double which is simpler and faster. 就个人而言,我更喜欢使用double ,它更简单,更快。 The only down side is you have to manage the rounding yourself. 唯一的缺点是您必须自己管理舍入。

double div = value1 / value2;
if (0 < div && div < SOME_UPPER_SENSIBLE_LIMIT) {
   // division was ok.
   System.out.printf("%.4f", div); // print with rounding.

Use this instead. 改用它。

  if(BigDecimal.ZERO.compareTo(value1) != 0 && BigDecimal.ZERO.compareTo(value2) != 0) 
  {
     ....
  }

equals will not work. equals将不起作用。

Check this: public boolean equals(Object x) Compares this BigDecimal with the specified Object for equality. 检查以下内容: public boolean equals(Object x)将此BigDecimal与指定的Object比较是否相等。 Unlike compareTo, this method considers two BigDecimal objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method). 与compareTo不同,此方法仅在两个BigDecimal对象的值和比例相等时才认为它们相等(由此方法比较时,2.0不等于2.00)。

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

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