首页 > 代码库 > MathUtils BigDecimal 数字工具类

MathUtils BigDecimal 数字工具类

package com.hxqc.basic.dependency.util;

import org.apache.commons.lang.StringUtils;

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

/**
 * 数字 工具
 *
 * @author huyj
 * @Created 2017-02-27 21:51
 */
public class MathUtils {

    public static BigDecimal getBigDecimal(Object value) {
        //System.out.println("位置:MathUtils.getBigDecimal ==> "+"[value.toString() = "+value.toString()+"]");
        BigDecimal ret = null;
        if (value != null) {
            if (value instanceof BigDecimal) {
                ret = (BigDecimal) value;
            } else if (value instanceof String) {
                value = StringUtils.isNotBlank((String)value) ? value : "0" ;
                ret = new BigDecimal((String) value);
            } else if (value instanceof BigInteger) {
                ret = new BigDecimal((BigInteger) value);
            } else if (value instanceof Number) {
                
                ret = new BigDecimal(String.valueOf(value));
            } else {
                //throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass() + " into a BigDecimal.");
                ret = BigDecimal.ZERO;
            }
        }else{
            ret = BigDecimal.ZERO;
        }
        return ret;
    }
}

 

MathUtils BigDecimal 数字工具类