码迷,mamicode.com
首页 > 其他好文 > 详细

[类型转换] Object 转换为 BigDecimal

时间:2018-08-13 12:19:01      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:return   class   cep   static   oss   public   null   tcl   number   

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

  

[类型转换] Object 转换为 BigDecimal

标签:return   class   cep   static   oss   public   null   tcl   number   

原文地址:https://www.cnblogs.com/smdq/p/9466895.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!