package com.gyee.common.util;

import java.math.BigDecimal;

/**
 * @ClassName : DoubleUtils
 * @Author : xieshengjie
 * @Date: 2021/11/16 17:00
 * @Description :
 */
public class DoubleUtils {

    /**
     * 保留两位小数
     *
     * @param number
     * @param precision
     * @return
     */
    public static double keepPrecision(double number, int precision) {
        BigDecimal bg = new BigDecimal(number);
        return bg.setScale(precision, BigDecimal.ROUND_HALF_UP).doubleValue();
    }


    /**
     * double保留小数点位数，四舍五入
     * 比如处理价格等数据
     *
     * @param d 要处理的double值
     * @param n 要保留的位数
     * @return
     */
    public static double getRoundingNum(double d, int n) {
        BigDecimal b = new BigDecimal(String.valueOf(d));
        b = b.divide(BigDecimal.ONE, n, BigDecimal.ROUND_HALF_UP);
        return b.doubleValue();
    }

    /**
     * @param numA    数字A
     * @param numB    数字B
     * @param operate 运算符
     * @return
     */
    public static double GetResult(double numA, double numB, String operate) {
        double res = 0;
        BigDecimal bigA = new BigDecimal(Double.toString(numA));
        BigDecimal bigB = new BigDecimal(Double.toString(numB));
        switch (operate) {

            case "+":
                res = bigA.add(bigB).doubleValue();
                break;
            case "-":
                res = bigA.subtract(bigB).doubleValue();
                break;
            case "*":
                res = bigA.multiply(bigB).doubleValue();
                break;
            case "/":
                res = bigA.divide(bigB, 2, BigDecimal.ROUND_HALF_UP).doubleValue();
                break;
            default:
                System.out.println("运算符不合法~");
                break;
        }
        return res;
    }


    public static Double sum(Double... in) {
        Double result = 0.0;
        for (int i = 0; i < in.length; i++) {
            result = result + (ifNullSet0(in[i]));
        }
        return result;
    }

    public static String hb(String... in) {
        String result = "";
        for (int i = 0; i < in.length; i++) {
            result = result + in[i];
        }
        return result;
    }

    public static Integer sum(Integer... in) {
        Integer result = 0;
        for (int i = 0; i < in.length; i++) {
            result = result + (ifNullSet0(in[i]));
        }
        return result;
    }

    public static Double max(Double... in) {
        Double result = 0.0;
        for (int i = 0; i < in.length; i++) {
            if (result < ifNullSet0(in[i])) {
                result = ifNullSet0(in[i]);
            }
        }
        return result;
    }

    public static Double min(Double... in) {
        Double result = 0.0;
        for (int i = 0; i < in.length; i++) {
            if (i == 0) {
                result = ifNullSet0(in[0]);
            }
            if (result > ifNullSet0(in[i])) {
                result = ifNullSet0(in[i]);
            }
        }
        return result;
    }

    public static Double ave(Double... in) {
        if (in.length == 0) {
            return 0.0;
        } else {
            return sum(in) / in.length;
        }

    }

    public static Double ifNullSet0(Double in) {
        if (in != null) {
            return in;
        }
        return 0.0;
    }

    public static Integer ifNullSet0(Integer in) {
        if (in != null) {
            return in;
        }
        return 0;
    }
}
