package com.jeeplus.modules.sg.financial.erpcredit.util;

import java.math.BigDecimal;

public class DoubleUtil {

    /**
     * 返回double保留两位小数点的值
     * @param num
     * @return
     */
    public static double round2(double num){
        return (double) Math.round(num * 100) / 100;
    }

    /**
     * 返回double类型 value1减去value2的准确值
     * @param value1
     * @param value2
     * @return
     */
    public static double sub(Double value1, Double value2) {
        value1 = value1 != null?value1:0.00;
        value2 = value2 != null?value2:0.00;
        BigDecimal b1 = new BigDecimal(Double.toString(value1));
        BigDecimal b2 = new BigDecimal(Double.toString(value2));
        return b1.subtract(b2).doubleValue();
    }

    /**
     * 返回double类型 获得value1加上value2的精确值
     * @param value1
     * @param value2
     * @return
     */
    public static double add(Double value1, Double value2) {
        value1 = value1 != null?value1:0.00;
        value2 = value2 != null?value2:0.00;
        BigDecimal b1 = new BigDecimal(Double.toString(value1));
        BigDecimal b2 = new BigDecimal(Double.toString(value2));
        return b1.add(b2).doubleValue();
    }

    /**
     * 返回多个Double的精确相加值
     * @param value1
     * @param values
     * @return
     */
    public static double add(Double value1,Double...values){
        value1 = value1 != null?value1:0.00;
        BigDecimal b1 = new BigDecimal(Double.toString(value1));
        for(int i=0;i<values.length;i++){
            Double value2 = values[i];
            value2 = value2!=null?value2:0.00;
            BigDecimal b2 = new BigDecimal(Double.toString(value2));
            b1 = b1.add(b2);
        }
        return b1.doubleValue();
    }

}
