[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;  
    }  
  
}

  

posted @ 2018-08-13 11:11  冰糖雪梨喵喵  阅读(1347)  评论(0编辑  收藏  举报