package com.zcxk.core.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author Andy
 * @version V1.0
 * @description: BigDecimal运算处理
 * @date 2021/7/21
 **/
public class BigDecimalUtils {


    public static String percentDivideDown(Object value, Object value2, int scale){
        return divideDown(value, value2, scale).multiply(new BigDecimal("100")).stripTrailingZeros().toPlainString() + "%";
    }

    public static String percentDivide(Object value, Object value2, int scale){
        return divide(value, value2, scale).multiply(new BigDecimal("100")).stripTrailingZeros().toPlainString() + "%";
    }

    /**
    * @author Andy
    * @description 不进行四舍五入，直接截取
    * @date 14:22 2021/7/26
    * @param value, value2, scale
    * @return java.math.BigDecimal
    **/
    public static BigDecimal divideDown(Object value, Object value2, int scale){
        return new BigDecimal(value.toString()).divide(new BigDecimal(value2.toString()), scale, RoundingMode.DOWN);
    }

    /**
    * @author Andy
    * @description 四舍五入
    * @date 14:22 2021/7/26
    * @param value, value2, scale
    * @return java.math.BigDecimal
    **/
    public static BigDecimal divide(Object value, Object value2, int scale){
        return new BigDecimal(value.toString()).divide(new BigDecimal(value2.toString()), scale, RoundingMode.HALF_UP);
    }

    public static double divideToDouble(Object value, Object value2, int scale){
        return divide(value, value2, scale).doubleValue();
    }

    public static BigDecimal setScale(Object value, int scale){
        return new BigDecimal(value.toString()).setScale(scale, BigDecimal.ROUND_HALF_UP);
    }


    public static double setScaleToDouble(Object value, int scale){
        return setScale(value, scale).doubleValue();
    }

    /**
    * @author Andy
    * @description 乘法
    * @date 9:38 2021/8/2
    * @param value, value2
    * @return java.math.BigDecimal
    **/
    public static BigDecimal multiply(Object value, Object value2){
        return multiply(new BigDecimal(value.toString()), new BigDecimal(value2.toString()), 2);
    }

    /**
    * 减法
    * @author Andy
    * @date 17:56 2021/8/11
    * @param value:
    * @param value2:
    * @return java.math.BigDecimal
    **/
    public static BigDecimal subtract(Object value, Object value2){
        return subtract(new BigDecimal(value.toString()), new BigDecimal(value2.toString()));
    }

    /**
     * 减法
     * @param value
     * @param value2
     * @return
     */
    public static BigDecimal subtract(BigDecimal value, BigDecimal value2){
        return value.subtract(value2);
    }

    /**
     * 乘法
     * @param value
     * @param value2
     * @return
     */
    public static BigDecimal multiply(BigDecimal value, BigDecimal value2, int scale){
        return value.multiply(value2).setScale(scale,BigDecimal.ROUND_HALF_UP);
    }


}
