Here you can find the source of toNumber(final BigDecimal bigDecimal, final Class
@SuppressWarnings("unchecked") public static <T extends Number> T toNumber(final BigDecimal bigDecimal, final Class<T> clazz)
//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; } } }