Java BigDecimal from toBigDecimal(Number number)

Here you can find the source of toBigDecimal(Number number)

Description

We should convert each number to java.math.BigDecimal to compare different number types: Byte, Short, Integer, Long, Float, Double, BigDecimal, etc

License

Open Source License

Parameter

Parameter Description
number Number to convert

Return

BigDecimal

Declaration

public static BigDecimal toBigDecimal(Number number) 

Method Source Code


//package com.java2s;
/*//from w ww.  j a  va  2 s .  co  m
 * Copyright (C) 2005 - 2014 TIBCO Software Inc. All rights reserved.
 * http://www.jaspersoft.com.
 *
 * Unless you have purchased  a commercial license agreement from Jaspersoft,
 * the following license terms  apply:
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License  as
 * published by the Free Software Foundation, either version 3 of  the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero  General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public  License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

import java.math.BigDecimal;
import java.math.BigInteger;

public class Main {
    /**
     * We should convert each number to {@link java.math.BigDecimal} to compare different number types: Byte, Short, Integer, Long, Float, Double, BigDecimal, etc
     * @param number Number to convert
     * @return BigDecimal
     */
    public static BigDecimal toBigDecimal(Number number) {
        if (number.getClass() == Integer.class || number.getClass() == Long.class
                || number.getClass() == Short.class || number.getClass() == Byte.class) {

            return BigDecimal.valueOf(number.longValue());

        } else if (number.getClass() == Double.class || number.getClass() == Float.class) {

            return BigDecimal.valueOf(number.doubleValue());

        } else if (number.getClass() == BigDecimal.class) {

            return (BigDecimal) number;

        } else if (number.getClass() == BigInteger.class) {

            return new BigDecimal((BigInteger) number);

        }

        try {
            return new BigDecimal(number.toString());
        } catch (final NumberFormatException e) {
            throw new RuntimeException("The given number (\"" + number + "\" of class "
                    + number.getClass().getName() + ") does not have a parseable string representation", e);
        }
    }
}

Related

  1. toBigDecimal(Number n)
  2. toBigDecimal(Number n)
  3. toBigDecimal(Number n)
  4. toBigDecimal(Number num)
  5. toBigDecimal(Number number)
  6. toBigDecimal(Number number)
  7. toBigDecimal(Number number)
  8. toBigDecimal(Number number, int scale)
  9. toBigDecimal(Number price)