[英]How to check if BigDecimal variable == 0 in java?
我在 Java 中有以下代碼;
BigDecimal price; // assigned elsewhere
if (price.compareTo(new BigDecimal("0.00")) == 0) {
return true;
}
編寫 if 條件的最佳方法是什么?
使用compareTo(BigDecimal.ZERO)
而不是equals()
:
if (price.compareTo(BigDecimal.ZERO) == 0) // see below
與BigDecimal
常量BigDecimal.ZERO
相比,避免了每次執行都必須構造一個new BigDecimal(0)
。
僅供參考, BigDecimal
還有常量BigDecimal.ONE
和BigDecimal.TEN
為您提供方便。
您不能使用BigDecimal#equals()
是它考慮了比例:
new BigDecimal("0").equals(BigDecimal.ZERO) // true
new BigDecimal("0.00").equals(BigDecimal.ZERO) // false!
所以它不適合純數字比較。 但是, BigDecimal.compareTo()
在比較時不考慮比例:
new BigDecimal("0").compareTo(BigDecimal.ZERO) == 0 // true
new BigDecimal("0.00").compareTo(BigDecimal.ZERO) == 0 // true
或者,可以使用signum() :
if (price.signum() == 0) {
return true;
}
您可以檢查一個常量:
someBigDecimal.compareTo(BigDecimal.ZERO) == 0
或者,我認為值得一提的是 BigDecimal 類中的 equals 和 compareTo 方法的行為彼此不一致。
這基本上意味着:
BigDecimal someValue = new BigDecimal("0.00");
System.out.println(someValue.compareTo(BigDecimal.ZERO) == 0); // true
System.out.println(someValue.equals(BigDecimal.ZERO)); // false
因此,你必須非常小心你的someValue
變量中的比例,否則你會得到意想不到的結果。
我通常使用以下方法:
if (selectPrice.compareTo(BigDecimal.ZERO) == 0) { ... }
你會想要使用equals()
因為它們是對象,並利用內置的ZERO
實例:
if (selectPrice.equals(BigDecimal.ZERO))
請注意, .equals()
考慮了比例,因此除非 selectPrice 與 .ZERO 的比例相同 (0), .ZERO
這將返回 false。
把比例從等式中去掉:
if (selectPrice.compareTo(BigDecimal.ZERO) == 0)
我應該注意,對於某些數學情況, 0.00 != 0
,這就是為什么我.equals()
考慮了比例。 0.00
精確到百分之一,而0
不是那么精確。 根據情況,您可能希望堅持使用.equals()
。
GriffeyDog 絕對正確:
代碼:
BigDecimal myBigDecimal = new BigDecimal("00000000.000000");
System.out.println("bestPriceBigDecimal=" + myBigDecimal);
System.out.println("BigDecimal.valueOf(0.000000)=" + BigDecimal.valueOf(0.000000));
System.out.println(" equals=" + myBigDecimal.equals(BigDecimal.ZERO));
System.out.println("compare=" + (0 == myBigDecimal.compareTo(BigDecimal.ZERO)));
結果:
myBigDecimal=0.000000
BigDecimal.valueOf(0.000000)=0.0
equals=false
compare=true
雖然我理解 BigDecimal 比較的優點,但我不會認為它是一個直觀的構造(例如 ==、<、>、<=、>= 運算符)。 當你腦子里想着一百萬件事情(好吧,七件事情)時,任何能減少認知負荷的事情都是好事。 所以我構建了一些有用的便利函數:
public static boolean equalsZero(BigDecimal x) {
return (0 == x.compareTo(BigDecimal.ZERO));
}
public static boolean equals(BigDecimal x, BigDecimal y) {
return (0 == x.compareTo(y));
}
public static boolean lessThan(BigDecimal x, BigDecimal y) {
return (-1 == x.compareTo(y));
}
public static boolean lessThanOrEquals(BigDecimal x, BigDecimal y) {
return (x.compareTo(y) <= 0);
}
public static boolean greaterThan(BigDecimal x, BigDecimal y) {
return (1 == x.compareTo(y));
}
public static boolean greaterThanOrEquals(BigDecimal x, BigDecimal y) {
return (x.compareTo(y) >= 0);
}
以下是它們的使用方法:
System.out.println("Starting main Utils");
BigDecimal bigDecimal0 = new BigDecimal(00000.00);
BigDecimal bigDecimal2 = new BigDecimal(2);
BigDecimal bigDecimal4 = new BigDecimal(4);
BigDecimal bigDecimal20 = new BigDecimal(2.000);
System.out.println("Positive cases:");
System.out.println("bigDecimal0=" + bigDecimal0 + " == zero is " + Utils.equalsZero(bigDecimal0));
System.out.println("bigDecimal2=" + bigDecimal2 + " < bigDecimal4=" + bigDecimal4 + " is " + Utils.lessThan(bigDecimal2, bigDecimal4));
System.out.println("bigDecimal2=" + bigDecimal2 + " == bigDecimal20=" + bigDecimal20 + " is " + Utils.equals(bigDecimal2, bigDecimal20));
System.out.println("bigDecimal2=" + bigDecimal2 + " <= bigDecimal20=" + bigDecimal20 + " is " + Utils.equals(bigDecimal2, bigDecimal20));
System.out.println("bigDecimal2=" + bigDecimal2 + " <= bigDecimal4=" + bigDecimal4 + " is " + Utils.lessThanOrEquals(bigDecimal2, bigDecimal4));
System.out.println("bigDecimal4=" + bigDecimal4 + " > bigDecimal2=" + bigDecimal2 + " is " + Utils.greaterThan(bigDecimal4, bigDecimal2));
System.out.println("bigDecimal4=" + bigDecimal4 + " >= bigDecimal2=" + bigDecimal2 + " is " + Utils.greaterThanOrEquals(bigDecimal4, bigDecimal2));
System.out.println("bigDecimal2=" + bigDecimal2 + " >= bigDecimal20=" + bigDecimal20 + " is " + Utils.greaterThanOrEquals(bigDecimal2, bigDecimal20));
System.out.println("Negative cases:");
System.out.println("bigDecimal2=" + bigDecimal2 + " == zero is " + Utils.equalsZero(bigDecimal2));
System.out.println("bigDecimal2=" + bigDecimal2 + " == bigDecimal4=" + bigDecimal4 + " is " + Utils.equals(bigDecimal2, bigDecimal4));
System.out.println("bigDecimal4=" + bigDecimal4 + " < bigDecimal2=" + bigDecimal2 + " is " + Utils.lessThan(bigDecimal4, bigDecimal2));
System.out.println("bigDecimal4=" + bigDecimal4 + " <= bigDecimal2=" + bigDecimal2 + " is " + Utils.lessThanOrEquals(bigDecimal4, bigDecimal2));
System.out.println("bigDecimal2=" + bigDecimal2 + " > bigDecimal4=" + bigDecimal4 + " is " + Utils.greaterThan(bigDecimal2, bigDecimal4));
System.out.println("bigDecimal2=" + bigDecimal2 + " >= bigDecimal4=" + bigDecimal4 + " is " + Utils.greaterThanOrEquals(bigDecimal2, bigDecimal4));
結果如下所示:
Positive cases:
bigDecimal0=0 == zero is true
bigDecimal2=2 < bigDecimal4=4 is true
bigDecimal2=2 == bigDecimal20=2 is true
bigDecimal2=2 <= bigDecimal20=2 is true
bigDecimal2=2 <= bigDecimal4=4 is true
bigDecimal4=4 > bigDecimal2=2 is true
bigDecimal4=4 >= bigDecimal2=2 is true
bigDecimal2=2 >= bigDecimal20=2 is true
Negative cases:
bigDecimal2=2 == zero is false
bigDecimal2=2 == bigDecimal4=4 is false
bigDecimal4=4 < bigDecimal2=2 is false
bigDecimal4=4 <= bigDecimal2=2 is false
bigDecimal2=2 > bigDecimal4=4 is false
bigDecimal2=2 >= bigDecimal4=4 is false
對於您的示例,一個簡單而更好的方法是:
BigDecimal price;
if(BigDecimal.ZERO.compareTo(price) == 0){
//Returns TRUE
}
只是想在這里分享一些對 kotlin 有用的擴展
fun BigDecimal.isZero() = compareTo(BigDecimal.ZERO) == 0
fun BigDecimal.isOne() = compareTo(BigDecimal.ONE) == 0
fun BigDecimal.isTen() = compareTo(BigDecimal.TEN) == 0
if(price.floatValue() == 0){
return true; //works for 0\0.0
}
BigDecimal.ZERO.setScale(2).equals(new BigDecimal("0.00"));
有一個代表 0的靜態常量:
BigDecimal.ZERO.equals(selectPrice)
你應該這樣做而不是:
selectPrice.equals(BigDecimal.ZERO)
為了避免selectPrice
為null
的情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.