1. 程式人生 > 實用技巧 >BigDecimal 類的 compareTo() 和 equals()方法

BigDecimal 類的 compareTo() 和 equals()方法

BigDecimal 類的 compareTo() 和 equals()方法

1. compareTo()原始碼

   /**
    * Compares this BigDecimal with the specified BigDecimal. 
    * Two BigDecimal objects that are equal in value but have 
    * a different scale (like 2.0 and 2.00) are considered equal
    * by this method. This method is provided in preference to 
    * individual methods for each of the six boolean comparison 
    * operators(<,<=,==,>,>=,!=). The suggested idiom for 
    * performing these comparisons is: x.compartTo(y) <op> 0, 
    * where <op> is one of the six comparison operators.
    * 
    * @param  val - BigDecimal to which this BigDecimal is to be 
    *         compared.
    * @return -1,0 or 1 as this BigDecimal is numerically less than,
    *         equal to, or greater than val.
    */
public int compareTo(BigDecimal val) {
    // Quick path for equal scale and non-inflated case.
    if (scale == val.scale) {
        long xs = intCompact;
        long ys = val.intCompact;
        if (xs != INFLATED && ys != INFLATED)
            return xs != ys ? ((xs > ys) ? 1 : -1) : 0;
    }
    int xsign = this.signum();
    int ysign = val.signum();
    if (xsign != ysign)
        return (xsign > ysign) ? 1 : -1;
    if (xsign == 0)
        return 0;
    int cmp = compareMagnitude(val);
    return (xsign > 0) ? cmp : -cmp;
}

1.1 註釋翻譯

將當前 BigDecimal 例項與給定 BigDecimal 例項進行比較。本方法認為,數值相同但精度不同(例如:2.0 和 2.00)的兩個 BigDecimal 是相同的。

本方法的返回值總共有3種:

  • -1 :當前 BigDecimal 的數值小於給定的 BigDecimal。
  • 0 :當前 BigDecimal 的數值等於給定的 BigDecimal。
  • 1 :當前 BigDecimal 的數值大於給定的 BigDecimal。

1.2 實現細節

2. equals()原始碼

/**
 * Compares this BigDecimal with the specified Object for equality. 
 * Unlike compareTo, thi 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).
 * 
 * @param  x - Object to which this BigDecimal is to to compared.
 * @return   - true if and only if the specified Object is a BigDecimal whose 
 *             value and scale are equal to this BigDecimal`s.
 */
@Override
public boolean equals(Object x) {
    if (!(x instanceof BigDecimal))
        return false;
    BigDecimal xDec = (BigDecimal) x;
    if (x == this)
        return true;
    if (scale != xDec.scale)
        return false;
    long s = this.intCompact;
    long xs = xDec.intCompact;
    if (s != INFLATED) {
        if (xs == INFLATED)
            xs = compactValFor(xDec.intVal);
        return xs == s;
    } else if (xs != INFLATED)
        return xs == compactValFor(this.intVal);

    return this.inflated().equals(xDec.inflated());
}

2.1 註釋翻譯

將當前 BigDecimal 例項與給定 BigDecimal 例項進行比較。本方法認為,只有數值和精度都相同的兩個 BigDecimal 才是相同的(因此本方法認為2.0 和 2.00 是不同的)。

本方法的返回值總共有2種:

  • true :當前 BigDecimal 的數值等於給定的 BigDecimal。
  • false:當前 BigDecimal 的數值不等於給定的 BigDecimal。

2.2 實現細節

3. 總結

BigDecimal 類的 compartTo 方法和 equals 方法都能用於比較 BigDecimal 例項代表的數值的大小。但兩個方法在判別兩個數值是否相等的標準上存在一定的差異:compartTo 僅要求兩個數的值(value)相等,equals 要求兩個數的值和精度(scale)都要相等。

public static void main(String[] args) {
    BigDecimal b1 = new BigDecimal("2.0");
    BigDecimal b2 = new BigDecimal("2.00");

    System.out.println("b1.compareTo(b2):" + (b1.compareTo(b2) == 0));
    System.out.println("b1.equals(b2):" + b1.equals(b2));
}
b1.compareTo(b2):true
b1.equals(b2):false

Process finished with exit code 0