Java BigDecimal Equal equals(BigDecimal left, BigDecimal right, int scale)

Here you can find the source of equals(BigDecimal left, BigDecimal right, int scale)

Description

equals

License

Open Source License

Declaration

static public boolean equals(BigDecimal left, BigDecimal right, int scale) 

Method Source Code


//package com.java2s;
import java.math.*;

public class Main {
    static public boolean equals(Object left, Object right, int scale) {

        if (left == null || right == null) {
            return (left == right);
        }/*from   w ww.  j  a v  a 2 s.  c  om*/

        return equals(left.toString(), right.toString(), scale);
    }

    static public boolean equals(String left, String right, int scale) {

        if (left == null || right == null) {
            return (left == right);
        }

        try {
            return equals(new BigDecimal(left), new BigDecimal(right), scale);
        } catch (Exception e) {
            // Ignored
        }

        return (left).equals(right);
    }

    static public boolean equals(BigDecimal left, BigDecimal right, int scale) {

        if (left == null || right == null) {
            return (left == right);
        } // End if

        if (scale <= 0) {
            scale = Math.min(left.scale(), right.scale());
        } else

        {
            scale = Math.min(Math.min(left.scale(), right.scale()), scale);
        } // End if

        left = limitScale(left, scale);
        right = limitScale(right, scale);

        return (left).compareTo(right) == 0;
    }

    static private BigDecimal limitScale(BigDecimal value, int scale) {

        if (value.scale() > scale) {
            value = value.setScale(scale, RoundingMode.HALF_UP);
        }

        return value;
    }
}

Related

  1. bigDecimalEqualsOrBothNull(BigDecimal obj1, BigDecimal obj2)
  2. equalBD(BigDecimal val1, BigDecimal val2)
  3. equals(BigDecimal a, BigDecimal b)
  4. equals(BigDecimal bd1, BigDecimal bd2)
  5. equals(BigDecimal decimal, double number)
  6. Equals(BigDecimal one, BigDecimal two)
  7. equals(final BigDecimal b0, final BigDecimal b1, final double delta)
  8. equals(final BigDecimal pValue1, final BigDecimal pValue2)
  9. equalsBigDecimal(BigDecimal bd1, BigDecimal bd2)