比較 BigDecimals

方法 compareTo 應該用來比較 BigDecimals

BigDecimal a = new BigDecimal(5);
a.compareTo(new BigDecimal(0));    // a is greater, returns 1
a.compareTo(new BigDecimal(5));    // a is equal, returns 0
a.compareTo(new BigDecimal(10));   // a is less, returns -1

通常,你應該使用 equals ,因為它考慮兩個 BigDecimals 僅相當於如果他們在價值相等和方法也擴充套件

BigDecimal a = new BigDecimal(5);
a.equals(new BigDecimal(5));       // value and scale are equal, returns true
a.equals(new BigDecimal(5.00));    // value is equal but scale is not, returns false