[Java类型转换] Object 转换为 BigDecimal
import java.math.BigDecimal; import java.math.BigInteger; public class MathUtil { public static BigDecimal getBigDecimal( Object value ) { BigDecimal val = null; if( value != null ) { if( value instanceof BigDecimal ) { val = (BigDecimal) value; } else if( value instanceof String ) { val = new BigDecimal( (String) value ); } else if( value instanceof BigInteger ) { val = new BigDecimal( (BigInteger) value ); } else if( value instanceof Number ) { val = new BigDecimal( ((Number)value).doubleValue() ); } else { throw new ClassCastException("Not possible to coerce ["+value+"] from class "+value.getClass()+" into a BigDecimal."); } } return val; } }
心之所向,素履以往~