Java BigDecimal Create toBigNumber(Number n)

Here you can find the source of toBigNumber(Number n)

Description

Converts the the Number n to either a BigInteger or a BigDecimal , unless the Number is a Float/Double +/- Infinity or NaN, in which case Double#NaN , Double#POSITIVE_INFINITY , or Double#NEGATIVE_INFINITY is returned.

License

Open Source License

Parameter

Parameter Description
n the Number to convert

Return

the or equivalent of n

Declaration

public static Number toBigNumber(Number n) 

Method Source Code


//package com.java2s;
/* Copyright (C) 2013-2014 Ian Teune <ian.teune@gmail.com>
 * //from   w  w  w .j  ava 2s  .c  o  m
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

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

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

public class Main {
    /**
     * Converts the the Number {@code n} to either a {@link BigInteger} or a
     * {@link BigDecimal}, unless the Number is a Float/Double +/- Infinity or
     * NaN, in which case {@link Double#NaN}, {@link Double#POSITIVE_INFINITY},
     * or {@link Double#NEGATIVE_INFINITY} is returned. 
     *
     * @param n the Number to convert
     * @return the {@link BigInteger} or {@link BigDecimal} equivalent of
     * {@code n}
     */
    public static Number toBigNumber(Number n) {
        if (n instanceof BigDecimal || n instanceof BigInteger) {
            return n;
        }
        if (n instanceof Float) {
            if (((Float) n).isInfinite()) {
                return n.equals(Float.POSITIVE_INFINITY) ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY;
            } else if (((Float) n).isNaN()) {
                return Double.NaN;
            } else {
                return new BigDecimal(n.toString());
            }
        }
        if (n instanceof Double) {
            if (((Double) n).isInfinite() || ((Double) n).isNaN()) {
                return n;
            } else {
                //had issues with BigDecimal.valueOf(double), so String constructor must be used
                return new BigDecimal(n.toString());
            }
        }
        if (n instanceof Integer || n instanceof Long || n instanceof Byte || n instanceof Short
                || n instanceof AtomicInteger || n instanceof AtomicLong) {
            return new BigInteger(n.toString());
        }
        return new BigDecimal(n.toString());
    }
}

Related

  1. toBigDecimal(final Object value)
  2. toBigDecimal(Object o)
  3. toBigDecimal(Object obj)
  4. toBigDecimal(String bigDecimal)
  5. toBigDecimal(String text)