public static Number coerceBoxed(Number numToCoerce,Class resultBoxedType){
  if (numToCoerce.getClass() == resultBoxedType) {
    return numToCoerce;
  }
  if (resultBoxedType == Double.class) {
    return numToCoerce.doubleValue();
  }
  if (resultBoxedType == Long.class) {
    return numToCoerce.longValue();
  }
  if (resultBoxedType == BigInteger.class) {
    return BigInteger.valueOf(numToCoerce.longValue());
  }
  if (resultBoxedType == BigDecimal.class) {
    if (JavaClassHelper.isFloatingPointNumber(numToCoerce)) {
      return new BigDecimal(numToCoerce.doubleValue());
    }
    return new BigDecimal(numToCoerce.longValue());
  }
  if (resultBoxedType == Float.class) {
    return numToCoerce.floatValue();
  }
  if (resultBoxedType == Integer.class) {
    return numToCoerce.intValue();
  }
  if (resultBoxedType == Short.class) {
    return numToCoerce.shortValue();
  }
  if (resultBoxedType == Byte.class) {
    return numToCoerce.byteValue();
  }
  throw new IllegalArgumentException("Cannot coerce to number subtype " + resultBoxedType.getName());
}

public static Number coerceBoxed ( Number numToCoerce , Class resultBoxedType ) { if ( numToCoerce . getClass ( ) == resultBoxedType ) { return numToCoerce ; } if ( resultBoxedType == Double . class ) { return numToCoerce . doubleValue ( ) ; } if ( resultBoxedType == Long . class ) { return numToCoerce . longValue ( ) ; } if ( resultBoxedType == BigInteger . class ) { return BigInteger . valueOf ( numToCoerce . longValue ( ) ) ; } if ( resultBoxedType == BigDecimal . class ) { if ( JavaClassHelper . isFloatingPointNumber ( numToCoerce ) ) { return new BigDecimal ( numToCoerce . doubleValue ( ) ) ; } return new BigDecimal ( numToCoerce . longValue ( ) ) ; } if ( resultBoxedType == Float . class ) { return numToCoerce . floatValue ( ) ; } if ( resultBoxedType == Integer . class ) { return numToCoerce . intValue ( ) ; } if ( resultBoxedType == Short . class ) { return numToCoerce . shortValue ( ) ; } if ( resultBoxedType == Byte . class ) { return numToCoerce . byteValue ( ) ; } throw new IllegalArgumentException ( "Cannot coerce to number subtype " + resultBoxedType . getName ( ) ) ; }
coerce the given number to the given type , assuming the type is a boxed type . allows coerce to lower resultion number . does ' t coerce to primitive types . < p > meant for statement compile - time use , not for runtime use .
