Java BigDecimal from toBigDecimal(Number number)

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

Description

Safely converts a number to a BigInteger.

License

Apache License

Parameter

Parameter Description
Number object to parse

Exception

Parameter Description
ArithmeticException on overflow

Return

parsed object

Declaration

public static BigDecimal toBigDecimal(Number number) 

Method Source Code

//package com.java2s;
/**/* www  . j  a v  a2  s  .c  o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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;
import java.math.BigInteger;

public class Main {
    /**
     * Safely converts a number to a BigInteger. Loss of precision may occur. Throws
     * an arithmetic exception upon overflow.
     * @param    Number    object to parse
     * @return             parsed object
     * @throws   ArithmeticException    on overflow
     */
    public static BigDecimal toBigDecimal(Number number) {

        /* Switch on class and convert to BigDecimal */

        String type = number.getClass().getSimpleName();
        BigDecimal parsed;

        switch (type) {

        case "Byte":
            Byte by = (Byte) number;
            parsed = new BigDecimal(by.doubleValue());
            break;

        case "Short":
            Short sh = (Short) number;
            parsed = new BigDecimal(sh.doubleValue());
            break;

        case "Integer":
            Integer in = (Integer) number;
            parsed = new BigDecimal(in.doubleValue());
            break;

        case "Long":
            Long lo = (Long) number;
            parsed = new BigDecimal(lo.doubleValue());
            break;

        case "Float":
            Float fl = (Float) number;
            parsed = new BigDecimal(fl.doubleValue());
            break;

        case "BigInteger":
            parsed = new BigDecimal(((BigInteger) number));
            break;

        case "BigDecimal":
            parsed = (BigDecimal) number;
            break;

        case "Double":
            Double db = (Double) number;
            parsed = new BigDecimal(db);
            break;

        default:
            throw new IllegalArgumentException(
                    "Invalid type: " + type + "\nData values " + "must extend the abstract Number class.");

        }

        return parsed;
    }

    /**
     * Safely converts an object to a BigInteger. Loss of precision may occur. Throws
     * an arithmetic exception upon overflow.
     * @param    Object    object to parse
     * @return             parsed object
     * @throws   ArithmeticException    on overflow
     */
    public static BigDecimal toBigDecimal(Object obj) {

        /* Switch on class and convert to BigDecimal */

        String type = obj.getClass().getSimpleName();
        BigDecimal parsed;

        switch (type) {

        case "Byte":
            Byte by = (Byte) obj;
            parsed = new BigDecimal(by.doubleValue());
            break;

        case "Short":
            Short sh = (Short) obj;
            parsed = new BigDecimal(sh.doubleValue());
            break;

        case "Integer":
            Integer in = (Integer) obj;
            parsed = new BigDecimal(in.doubleValue());
            break;

        case "Long":
            Long lo = (Long) obj;
            parsed = new BigDecimal(lo.doubleValue());
            break;

        case "Float":
            Float fl = (Float) obj;
            parsed = new BigDecimal(fl.doubleValue());
            break;

        case "BigInteger":
            parsed = new BigDecimal(((BigInteger) obj));
            break;

        case "BigDecimal":
            parsed = (BigDecimal) obj;
            break;

        case "Double":
            Double db = (Double) obj;
            parsed = new BigDecimal(db);
            break;

        default:
            throw new IllegalArgumentException(
                    "Invalid type: " + type + "\nData values " + "must extend the abstract Number class.");

        }

        return parsed;
    }
}

Related

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