From d12843664d271ce628017f0c4b7ae8ea3ebecd10 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 3 Sep 2023 18:40:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=20https://github.com/YunaiV/?= =?UTF-8?q?ruoyi-vue-pro/pull/334?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yudao/module/pay/util/MoneyUtils.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/util/MoneyUtils.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/util/MoneyUtils.java index 76cf5bfa3..15865f60a 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/util/MoneyUtils.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/util/MoneyUtils.java @@ -1,5 +1,7 @@ package cn.iocoder.yudao.module.pay.util; +import cn.hutool.core.util.NumberUtil; + import java.math.BigDecimal; import java.math.RoundingMode; @@ -18,10 +20,20 @@ public class MoneyUtils { * @return 百分比金额 */ public static Integer calculateRatePrice(Integer price, Double rate) { - return new BigDecimal(price) - .multiply(BigDecimal.valueOf(rate)) // 乘以 - .setScale(0, RoundingMode.HALF_UP) // 四舍五入 - .intValue(); + return calculateRatePrice(price, rate, 0, RoundingMode.HALF_UP).intValue(); + } + + /** + * 计算百分比金额 + * + * @param price 金额 + * @param rate 百分比,例如说 56.77% 则传入 56.77 + * @param scale 保留小数位数 + * @param roundingMode 舍入模式 + */ + public static BigDecimal calculateRatePrice(Number price, Number rate, int scale, RoundingMode roundingMode) { + return NumberUtil.toBigDecimal(price).multiply(NumberUtil.toBigDecimal(rate)) // 乘以 + .divide(BigDecimal.valueOf(100), scale, roundingMode); // 除以 100 } }