java数字处理工具类


import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Map;

import org.apache.commons.jexl2.*;

public class NumberTool {
    private static final int FourDecimalMedian = 4;  //小数位数

    /**
     * 将object转换成整型,当传入的对象是null时返回指定的值
     * @param o
     * @param dv
     * @return
     */
    public static Integer safeToInteger(Object o, Integer dv){
        Integer r = dv;
        if (o != null){
            try{
                r = new Integer(String.valueOf(o));
            }
            catch (Exception ex){}
        }
        return r;
    }

    /**
     * 将object转换成整型,当传入的对象是null时返回指定的值
     * @param o
     * @param dv
     * @return
     */
    public static Double safeToDouble(Object o, Double dv){
        Double r = dv;
        if (o != null){
            try{
                r = new Double(String.valueOf(o));
            }
            catch(Exception ex){}
        }
        return r;
    }

    /**
     * 将object转换成整型,当传入的对象是null时返回指定的值
     * @param o
     * @param dv
     * @return
     */
    public static Float safeToFloat(Object o, Float dv){
        Float r = dv;
        if (o != null){
            try{
                r = new Float(String.valueOf(o));
            }
            catch(Exception ex){}
        }
        return r;
    }

    /**
     * String类型 转 BigDecimal类型
     *
     * @return paraValue
     */
    public static BigDecimal stringToBigDecimal(String paraValue) {
        try {
            BigDecimal bDecimal = null;
            if (paraValue.indexOf("%") == -1) {
                return new BigDecimal(Double.valueOf(paraValue.trim().replace(",", ""))).setScale(4, BigDecimal.ROUND_HALF_UP);
            } else {
                return new BigDecimal(Double.valueOf(paraValue.trim().replace("%", "")) / 100).setScale(FourDecimalMedian, BigDecimal.ROUND_HALF_UP);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * String类型 转 BigDecimal类型
     *
     * @return paraValue
     */
    public static BigDecimal stringToBigDecimalTwoSpilt(String paraValue) {
        try {
            BigDecimal bDecimal = null;
            if (paraValue.indexOf("%") == -1) {
                return new BigDecimal(Double.valueOf(paraValue.trim().replace(",", ""))).setScale(2, BigDecimal.ROUND_HALF_UP);
            } else {
                return new BigDecimal(Double.valueOf(paraValue.trim().replace("%", "")) / 100).setScale(FourDecimalMedian, BigDecimal.ROUND_HALF_UP);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * aParaValue值 - bParaValue值
     *
     * @return BigDecimal
     */
    public static BigDecimal aSubtractB(BigDecimal aParaValue, BigDecimal bParaValue) {
        try {
            return aParaValue.subtract(bParaValue).setScale(FourDecimalMedian, BigDecimal.ROUND_HALF_UP);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * aParaValue值 / bParaValue值
     *
     * @return BigDecimal
     */
    public static BigDecimal aDivideB(BigDecimal aParaValue, BigDecimal bParaValue) {
        try {
            return aParaValue.divide(bParaValue, FourDecimalMedian).setScale(FourDecimalMedian, BigDecimal.ROUND_HALF_UP);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static BigDecimal formulaCalculation(String _formula, Map _formulaProperties, String[] indList) {
        JexlContext _jexlContext = new MapContext();
        for (String _indicatorCode : indList) {
            _jexlContext.set("a" + _indicatorCode, (BigDecimal) _formulaProperties.get(_indicatorCode));
            _formula = _formula.replaceFirst(_indicatorCode, "a" + _indicatorCode);
        }
        JexlEngine _jexlEngine = new JexlEngine();
        Expression _expression = _jexlEngine.createExpression(_formula);
        Object obj = (Object) _expression.evaluate(_jexlContext);
        if (obj.getClass().toString().indexOf("Double") != -1) {
            Double result = (Double) obj;
            return BigDecimal.valueOf(result);
        }
        return (BigDecimal) _expression.evaluate(_jexlContext);
    }

    public static BigDecimal formulaCalculationDouble(String _formula, Map _formulaProperties, String[] indList) {
        JexlContext _jexlContext = new MapContext();
        for (String _indicatorCode : indList) {
            _jexlContext.set("a" + _indicatorCode, (BigDecimal) _formulaProperties.get(_indicatorCode));
            _formula = _formula.replaceFirst(_indicatorCode, "a" + _indicatorCode);
        }
        JexlEngine _jexlEngine = new JexlEngine();
        Expression _expression = _jexlEngine.createExpression(_formula);
        Double returnVal = (Double) _expression.evaluate(_jexlContext);
        return BigDecimal.valueOf(returnVal);
    }



    public static double avg(double[] sequenceArr) {
        if (sequenceArr != null && sequenceArr.length > 0) {
            double sum = 0.00;
            int T = sequenceArr.length;
            for (int i = 0; i < T; i++) {
                sum += sequenceArr[i];
            }
            return sum / T;
        }
        return 0.00;
    }

    public static double round(double value, int count) {
        int t = (int) Math.pow(10.0, (double) (count + 1));
        return ((double) Math.round(value * t)) / t;
    }


    public static Double getFourDecimalNumber(Number number) {
        if (number == null || (0.00 == number.doubleValue())) {
            return 0.0000;
        }
        NumberFormat format = new DecimalFormat("##0.0000");
        return Double.valueOf(format.format(number));
    }

    /**
     * 从指定Map中获取指定Key的 值,并将期值转为Integer型,若Map中指定Key不存在,则直接返回默认值
     *
     * @param map          Map
     * @param key          Key
     * @param defaultValue 默认值
     * @return 将Map值转换后的Integer值,若不存在则返回默认值
     */
    public static Integer convertMapKeyToInt(Map map, String key, Integer defaultValue) {
        return safeToInteger(map.get(key), defaultValue);
    }

    /**
     * 从指定Map中获取指定Key的值,并将期值转为Integer型,若Map中指定Key不存在,则直接返回0
     *
     * @param map Map
     * @param key Key
     * @return 将Map值转换后的Integer值,若不存在则返回0
     */
    public static Integer convertMapKeyToInt(Map map, String key) {
        return convertMapKeyToInt(map, key, 0);
    }

    /**
     * 将int数组转换成以逗号分隔的字符串
     * @param intArray
     * @return
     */
    public static String convertIntArrayToString(int[] intArray)
    {
        if (intArray.length <= 0)
            return "";
        String _string = "";
        for (int _int : intArray) {
            _string += _string.equals("") ? new Integer(_int).toString() : "," + new Integer(_int).toString() ;
        }
        return _string;
    }
}

你可能感兴趣的:(java)