Java BigDecimal Create bigDecimalValueOf(Number n)

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

Description

Converts a number into its BigDecimal equivalent.

License

Apache License

Parameter

Parameter Description
n an instance of java.lang.Number to be converted.

Return

BigDecimal equivalent of n.

Declaration

public static BigDecimal bigDecimalValueOf(Number n) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    /**/*  w ww  .  j  av  a  2  s .c o m*/
     * Converts a number into its BigDecimal equivalent. Useful for comparisons
     * between Numbers.
     * 
     * @param n an instance of java.lang.Number to be converted.
     * @return BigDecimal equivalent of n.
     */
    public static BigDecimal bigDecimalValueOf(Number n) {
        if (n instanceof BigDecimal) {
            return (BigDecimal) n;
        } else if (n instanceof BigInteger) {
            return new BigDecimal((BigInteger) n);
        } else if (n instanceof Double) {
            return new BigDecimal((Double) n);
        } else if (n instanceof Float) {
            return new BigDecimal((Float) n);
        } else {
            return n == null ? null : new BigDecimal(n.longValue());
        }
    }
}

Related

  1. bigDecimal(Number num)
  2. bigDecimal(Object object)
  3. bigDecimalFromBytes(byte[] decimalBytes, int scale)
  4. bigDecimalFromString(String s)
  5. bigDecimalValue(final Number number)
  6. createBigDecimal(double v)
  7. createBigDecimal(final String value)
  8. createBigDecimal(Object value)
  9. createBigDecimal(String val)