package com.lframework.xingyun.sc.util;

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

public class CalUtils {

    // 计算毛利率 = 本单利润/含税总金额* 100%
    public static String CalGrossMargin(BigDecimal thisOrderProfit, BigDecimal totalAmount) {
        if (totalAmount == null || totalAmount.compareTo(BigDecimal.ZERO) == 0) {
            return "0.000%";
        }

        BigDecimal margin = thisOrderProfit
                .divide(totalAmount, 6, RoundingMode.HALF_UP) // 增加到6位小数
                .multiply(BigDecimal.valueOf(100))
                .setScale(3, RoundingMode.HALF_UP);

        return margin.stripTrailingZeros().toPlainString() + "%";
    }
}

