Java BigDecimal Equal equals(final BigDecimal b0, final BigDecimal b1, final double delta)

Here you can find the source of equals(final BigDecimal b0, final BigDecimal b1, final double delta)

Description

Null safe equals with accepted delta.

License

Open Source License

Parameter

Parameter Description
b0 the first BigDecimal .
b1 the second BigDecimal .
delta the accepted variance to be interpreted as equal.

Return

equal or not.

Declaration

public static boolean equals(final BigDecimal b0, final BigDecimal b1, final double delta) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013, 2014, 2015 QPark Consulting  S.a r.l.
 * /*  w w w . java2s  .c  o m*/
 * This program and the accompanying materials are made available under the 
 * terms of the Eclipse Public License v1.0. 
 * The Eclipse Public License is available at 
 * http://www.eclipse.org/legal/epl-v10.html.
 ******************************************************************************/

import java.math.BigDecimal;

public class Main {
    /**
     * Null safe equals with accepted delta.
     *
     * @param b0
     *            the first {@link BigDecimal}.
     * @param b1
     *            the second {@link BigDecimal}.
     * @param delta
     *            the accepted variance to be interpreted as equal.
     * @return equal or not.
     */
    public static boolean equals(final BigDecimal b0, final BigDecimal b1, final double delta) {
        if (b0 == b1) {
            return true;
        } else if (b0 == null || b1 == null) {
            return false;
        } else {
            return b0.subtract(b1).abs().compareTo(BigDecimal.valueOf(Math.abs(delta))) <= 0;
        }
    }

    /**
     * Equals with accepted delta.
     *
     * @param d0
     *            the first {@link BigDecimal}.
     * @param d1
     *            the second {@link BigDecimal}.
     * @param delta
     *            the accepted variance to be interpreted as equal.
     * @return equal or not.
     */
    public static boolean equals(final double d0, final double d1, final double delta) {
        return Math.abs(d0 - d1) <= Math.abs(delta);
    }

    /**
     * Null safe equals of two {@link BigDecimal}s.
     *
     * @param b0
     *            the first {@link BigDecimal}.
     * @param b1
     *            the second {@link BigDecimal}.
     * @return equal or not
     * @see BigDecimal#equals(Object)
     */
    public static boolean equals(final BigDecimal b0, final BigDecimal b1) {
        if (b0 == b1) {
            return true;
        } else if (b0 == null || b1 == null) {
            return false;
        } else {
            return b0.equals(b1);
        }
    }

    /**
     * Null safe compareTo of two {@link BigDecimal}s.
     *
     * @param b0
     *            the first {@link BigDecimal}.
     * @param b1
     *            the second {@link BigDecimal}.
     * @return equal, more little or greater.
     * @see BigDecimal#compareTo(Object)
     */
    public static int compareTo(final BigDecimal b0, final BigDecimal b1) {
        if (b0 == b1) {
            return 0;
        } else if (b0 == null) {
            return 1;
        } else if (b1 == null) {
            return -1;
        } else {
            return b0.compareTo(b1);
        }
    }

    /**
     * Null safe compareTo of two {@link BigDecimal}s.
     *
     * @param d0
     *            the first {@link BigDecimal}.
     * @param d1
     *            the second {@link BigDecimal}.
     * @return equal, more little or greater.
     * @see BigDecimal#compareTo(Object)
     */
    public static int compareTo(final Double d0, final Double d1) {
        if (d0 == d1) {
            return 0;
        } else if (d0 == null) {
            return 1;
        } else if (d1 == null) {
            return -1;
        } else {
            return d0.compareTo(d1);
        }
    }
}

Related

  1. equals(BigDecimal a, BigDecimal b)
  2. equals(BigDecimal bd1, BigDecimal bd2)
  3. equals(BigDecimal decimal, double number)
  4. equals(BigDecimal left, BigDecimal right, int scale)
  5. Equals(BigDecimal one, BigDecimal two)
  6. equals(final BigDecimal pValue1, final BigDecimal pValue2)
  7. equalsBigDecimal(BigDecimal bd1, BigDecimal bd2)
  8. isEqual(BigDecimal aLhs, BigDecimal aRhs)
  9. isEqual(BigDecimal n1, BigDecimal n2, int precision)