Skip to content

Java语法之判断BigDecimal为0

java
BigDecimal.ZERO.equals(money);
BigDecimal.ZERO.equals(money);

需要额外注意: Bigdecimal的equals方法先比较scale。不仅仅比较值的大小是否相等。

java
BigDecimal x = new BigDecimal("1");
BigDecimal y = new BigDecimal("1.00");
System.out.println(x.equals(y));  // false
System.out.println(x.compareTo(y) == 0 ? "true": "false"); // true
BigDecimal x = new BigDecimal("1");
BigDecimal y = new BigDecimal("1.00");
System.out.println(x.equals(y));  // false
System.out.println(x.compareTo(y) == 0 ? "true": "false"); // true

BigDecimal (Java Platform SE 6)
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).