YunaiV 2023-09-03 18:40:58 +08:00
parent d04001a56d
commit d12843664d
1 changed files with 16 additions and 4 deletions

View File

@ -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
}
}