Java BigDecimal to toNumber(final BigDecimal bigDecimal, final Class clazz)

Here you can find the source of toNumber(final BigDecimal bigDecimal, final Class clazz)

Description

to Number

License

Apache License

Declaration

@SuppressWarnings("unchecked")
    public static <T extends Number> T toNumber(final BigDecimal bigDecimal, final Class<T> clazz) 

Method Source Code


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

import java.math.BigDecimal;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T extends Number> T toNumber(final BigDecimal bigDecimal, final Class<T> clazz) {
        if (bigDecimal == null) {
            return null;
        } else if (BigDecimal.class.isAssignableFrom(clazz)) {
            return (T) bigDecimal;
        } else if (Byte.class.isAssignableFrom(clazz)) {
            return (T) new Byte(bigDecimal.byteValue());
        } else if (Short.class.isAssignableFrom(clazz)) {
            return (T) new Short(bigDecimal.shortValue());
        } else if (Integer.class.isAssignableFrom(clazz)) {
            return (T) new Integer(bigDecimal.intValue());
        } else if (Long.class.isAssignableFrom(clazz)) {
            return (T) new Long(bigDecimal.longValue());
        } else if (Float.class.isAssignableFrom(clazz)) {
            return (T) new Float(bigDecimal.floatValue());
        } else if (Double.class.isAssignableFrom(clazz)) {
            return (T) new Double(bigDecimal.doubleValue());
        } else {//from w ww  . j  a v a  2s  .  co  m
            return null;
        }
    }
}

Related

  1. toDouble(final BigDecimal b)
  2. toDouble(final BigDecimal b)
  3. toInteger(BigDecimal b)
  4. toLong(BigDecimal value)
  5. toLongObject(@Nullable final BigDecimal value)
  6. toObject(BigDecimal value)
  7. toPercent(final BigDecimal decimalValue, final MathContext mathCntext)
  8. toScientificNotation(BigDecimal bd)
  9. toSimpleBigDecimal(Object num)