|
@@ -1,5 +1,6 @@
|
|
|
package cn.iocoder.yudao.framework.common.util.number;
|
|
|
|
|
|
+import cn.hutool.core.math.Money;
|
|
|
import cn.hutool.core.util.NumberUtil;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
@@ -16,7 +17,7 @@ public class MoneyUtils {
|
|
|
* 计算百分比金额,四舍五入
|
|
|
*
|
|
|
* @param price 金额
|
|
|
- * @param rate 百分比,例如说 56.77% 则传入 56.77
|
|
|
+ * @param rate 百分比,例如说 56.77% 则传入 56.77
|
|
|
* @return 百分比金额
|
|
|
*/
|
|
|
public static Integer calculateRatePrice(Integer price, Double rate) {
|
|
@@ -27,24 +28,43 @@ public class MoneyUtils {
|
|
|
* 计算百分比金额,向下传入
|
|
|
*
|
|
|
* @param price 金额
|
|
|
- * @param rate 百分比,例如说 56.77% 则传入 56.77
|
|
|
+ * @param rate 百分比,例如说 56.77% 则传入 56.77
|
|
|
* @return 百分比金额
|
|
|
*/
|
|
|
public static Integer calculateRatePriceFloor(Integer price, Double rate) {
|
|
|
return calculateRatePrice(price, rate, 0, RoundingMode.FLOOR).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
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 计算百分比金额
|
|
|
+ *
|
|
|
+ * @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
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 分转元
|
|
|
+ *
|
|
|
+ * @param fen 分
|
|
|
+ * @return 元
|
|
|
+ */
|
|
|
+ public static BigDecimal fenToYuan(int fen) {
|
|
|
+ return new Money(0, fen).getAmount();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分转元(字符串)
|
|
|
+ *
|
|
|
+ * @param fen 分
|
|
|
+ * @return 元
|
|
|
+ */
|
|
|
+ public static String fenToYuanStr(int fen) {
|
|
|
+ return new Money(0, fen).toString();
|
|
|
+ }
|
|
|
}
|