Java BigDecimal Value Check isValueEquals(BigDecimal decimal1, BigDecimal decimal2)

Here you can find the source of isValueEquals(BigDecimal decimal1, BigDecimal decimal2)

Description

e.g.

License

Apache License

Declaration

public static boolean isValueEquals(BigDecimal decimal1, BigDecimal decimal2) 

Method Source Code


//package com.java2s;
/*/*from   ww w . ja va2  s .  c o  m*/
 * Copyright 2013-2016 iNeunet OpenSource and the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.math.BigDecimal;

public class Main {
    /**
     * e.g. 0.30 equals 0.3 && 0.30 equals 0.30 && 0.30 equals 0.300.
     */
    public static boolean isValueEquals(BigDecimal decimal1, BigDecimal decimal2) {
        if (decimal1 == null && decimal2 == null)
            return true;
        if (decimal1 == null && decimal2 != null)
            return false;
        if (decimal1 != null && decimal2 == null)
            return false;
        return cutInvalidSacle(decimal1).equals(cutInvalidSacle(decimal2));
    }

    /**
     * Cut invalid scale.
     * @param val e.g. 0.350, its scale is 3
     * @return 0.35, its scale is 2.
     */
    public static BigDecimal cutInvalidSacle(BigDecimal val) {
        if (val == null)
            return null;
        double d = val.doubleValue();
        int i = val.intValue();
        if (d == i)
            return new BigDecimal(i);
        return new BigDecimal(Double.toString(d));
    }
}

Related

  1. isSameValue(final BigDecimal val1, final BigDecimal val2)
  2. isStrictementNegatif(BigDecimal pNombre)
  3. isUnscaledValueEqualsOriginal(BigDecimal bigDecimal, BigInteger bigInteger)
  4. isValidDecimal(BigDecimal bd)
  5. isValidLongitude(BigDecimal value)
  6. isZero(BigDecimal bd)
  7. isZero(BigDecimal d)
  8. isZero(BigDecimal decimal)
  9. isZero(BigDecimal inValue)