package cn.reghao.tnb.sb.bayes.income;

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

/**
 * @author reghao
 * @date 2025-08-25 11:31:54
 */
public class DecimalCalculator {
    public static double add(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.add(b2).doubleValue();
    }

    public static double subtract(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.subtract(b2).doubleValue();
    }

    public static double multiply(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.multiply(b2).doubleValue();
    }

    public static double divide(double v1, double v2) {
        // 保留两位小数, 向上取整
        int scale = 2;
        RoundingMode roundingMode = RoundingMode.HALF_UP;

        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.divide(b2, scale, roundingMode).doubleValue();
    }

    public static double divide(double v1, double v2, int scale) {
        // 保留 scale 位小数, 向上取整
        RoundingMode roundingMode = RoundingMode.HALF_UP;

        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.divide(b2, scale, roundingMode).doubleValue();
    }
}
