Java BigDecimal Compare compareBigDecimals(java.math.BigDecimal one, java.math.BigDecimal two)

Here you can find the source of compareBigDecimals(java.math.BigDecimal one, java.math.BigDecimal two)

Description

Compare two BigDecimals.

License

Open Source License

Declaration

public static boolean compareBigDecimals(java.math.BigDecimal one,
        java.math.BigDecimal two) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 1998, 2015 Oracle and/or its affiliates. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
 * which accompanies this distribution./*from www .  j a v  a 2  s.  co m*/
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     Oracle - initial API and implementation from Oracle TopLink
 *     dminsky - added countOccurrencesOf(Object, List) API
 *     08/23/2010-2.2 Michael O'Brien
 *        - 323043: application.xml module ordering may cause weaving not to occur causing an NPE.
 *                       warn if expected "_persistence_*_vh" method not found
 *                       instead of throwing NPE during deploy validation.
 ******************************************************************************/

public class Main {
    /**
     * Compare two BigDecimals.
     * This is required because the .equals method of java.math.BigDecimal ensures that
     * the scale of the two numbers are equal. Therefore 0.0 != 0.00.
     * @see java.math.BigDecimal#equals(Object)
     */
    public static boolean compareBigDecimals(java.math.BigDecimal one,
            java.math.BigDecimal two) {
        if (one.scale() != two.scale()) {
            double doubleOne = (one).doubleValue();
            double doubleTwo = (two).doubleValue();
            if ((doubleOne != Double.POSITIVE_INFINITY)
                    && (doubleOne != Double.NEGATIVE_INFINITY)
                    && (doubleTwo != Double.POSITIVE_INFINITY)
                    && (doubleTwo != Double.NEGATIVE_INFINITY)) {
                return doubleOne == doubleTwo;
            }
        }
        return one.equals(two);
    }
}

Related

  1. compare(BigDecimal decimal, Integer i)
  2. compare(BigDecimal first, BigDecimal second)
  3. compare(BigDecimal one, BigDecimal other)
  4. compare(BigDecimal v1, BigDecimal v2)
  5. compareBigDecimal(BigDecimal bigDecimal, BigDecimal bigDecimal2, boolean desc)
  6. compareTo(BigDecimal b1, BigDecimal b2)
  7. compareTo(final BigDecimal b0, final BigDecimal b1)
  8. compareTo(final BigDecimal v1, final BigDecimal v2)
  9. compareToOne(BigDecimal x)