package com.sckw.payment.utils;

import com.sckw.core.model.constant.NumberConstant;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Objects;

/**
 * @author xucaiqin
 * @date 2023-07-31 15:25:57
 */
public class DecimalUtils {
    private final static DecimalFormat df = new DecimalFormat("0.####");

    /**
     * @param dividend 被除数
     * @param divisor  除数
     * @return 百分比数据
     */
    public static String percent(BigDecimal dividend, BigDecimal divisor) {
        if (Objects.isNull(dividend)) {
            dividend = NumberConstant.ZERO_TWO;
        }
        //除数为0
        if (Objects.isNull(divisor)) {
            divisor = NumberConstant.ZERO_TWO;
        }

        if (BigDecimal.ZERO.compareTo(dividend) != 0 && BigDecimal.ZERO.compareTo(divisor) == 0) {
            return "100.00%";
        }
        if (BigDecimal.ZERO.compareTo(dividend) == 0 && BigDecimal.ZERO.compareTo(divisor) == 0) {
            return "0.00%";
        }
        return df.format(dividend.divide(divisor, 6, RoundingMode.HALF_UP).multiply(new BigDecimal("100.0")).abs()) + "%";
    }

    public static String financePercent(BigDecimal dividend, BigDecimal divisor) {
        if (Objects.isNull(dividend)) {
            dividend = NumberConstant.ZERO_TWO;
        }
        //除数为0
        if (Objects.isNull(divisor)) {
            divisor = NumberConstant.ZERO_TWO;
        }

        if (BigDecimal.ZERO.compareTo(dividend) != 0 && BigDecimal.ZERO.compareTo(divisor) == 0) {
            return "100.00%";
        }
        if (BigDecimal.ZERO.compareTo(dividend) == 0 && BigDecimal.ZERO.compareTo(divisor) == 0) {
            return "0.00%";
        }
        return df.format(dividend.divide(divisor, 6, RoundingMode.HALF_UP).multiply(new BigDecimal("100.0")).abs()) + "%";
    }
}
