项目中遇到读取Excel文件里面的数据转为金额的情况,为了程序更加的健壮,进行处理如下:

import java.math.BigDecimal;  
import java.math.BigInteger;  
  

public class MathUtils {  
      
    public static BigDecimal getBigDecimal( Object value ) {  
        BigDecimal ret = null;  
        if( value != null ) {  
            if( value instanceof BigDecimal ) {  
                ret = (BigDecimal) value;  
            } else if( value instanceof String ) {  
                ret = new BigDecimal( (String) value );  
            } else if( value instanceof BigInteger ) {  
                ret = new BigDecimal( (BigInteger) value );  
            } else if( value instanceof Number ) {  
                ret = new BigDecimal( ((Number)value).doubleValue() );  
            } else {  
                throw new ClassCastException("Not possible to coerce ["+value+"] from class "+value.getClass()+" into a BigDecimal.");  
            }  
        }  
        return ret;  
    }  
  
  
  
}  

 

相关文章:

  • 2021-10-19
  • 2022-02-06
  • 2021-07-27
  • 2022-12-23
  • 2021-12-30
  • 2022-12-23
  • 2021-11-18
  • 2022-12-23
猜你喜欢
  • 2021-11-23
  • 2022-12-23
  • 2021-07-17
  • 2021-11-18
  • 2022-12-23
  • 2021-12-11
相关资源
相似解决方案