Java BigDecimal Value Check isZero(final BigDecimal b)

Here you can find the source of isZero(final BigDecimal b)

Description

Null save comparison against BigDecimal#ZERO .

License

Open Source License

Parameter

Parameter Description
b the BigDecimal to compare.

Return

true if not null and equal to else false.

Declaration

public static boolean isZero(final BigDecimal b) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013, 2014, 2015 QPark Consulting  S.a r.l.
 * /*from  w  ww .java2s .com*/
 * 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 static java.math.BigDecimal.ZERO;
import java.math.BigDecimal;

public class Main {
    /**
     * Null save comparison against {@link BigDecimal#ZERO}.
     *
     * @param b
     *            the {@link BigDecimal} to compare.
     * @return <code>true</code> if not null and equal to
     *         {@link BigDecimal#ZERO} else <code>false</code>.
     */
    public static boolean isZero(final BigDecimal b) {
        if (b != null && b.equals(ZERO)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 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. isValueEquals(BigDecimal decimal1, BigDecimal decimal2)
  2. isZero(BigDecimal bd)
  3. isZero(BigDecimal d)
  4. isZero(BigDecimal decimal)
  5. isZero(BigDecimal inValue)
  6. isZero(final BigDecimal value)
  7. isZeroOrNull(final BigDecimal value)