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

  

转载于:https://www.cnblogs.com/smdq/p/9466895.html

你可能感兴趣的:([Java类型转换] Object 转换为 BigDecimal)