fix(Money): currency scaling bug

This commit is contained in:
asukavuuyn 2025-06-16 23:42:17 +08:00
parent 41141cd824
commit 6a58bfe9b2
2 changed files with 11 additions and 1 deletions

View File

@ -294,7 +294,7 @@ public class Money implements Serializable, Comparable<Money> {
*/
public void setAmount(BigDecimal amount) {
if (amount != null) {
cent = rounding(amount.movePointRight(2), DEFAULT_ROUNDING_MODE);
cent = rounding(amount.movePointRight(currency.getDefaultFractionDigits()), DEFAULT_ROUNDING_MODE);
}
}

View File

@ -2,6 +2,8 @@ package cn.hutool.core.math;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import java.util.Currency;
public class MoneyTest {
@ -20,4 +22,12 @@ public class MoneyTest {
assertEquals(1234.56D, MathUtil.centToYuan(123456), 0);
}
@Test
public void currencyScalingTest() {
Money jpyMoney = new Money(0, Currency.getInstance("JPY"));
jpyMoney.setAmount(BigDecimal.ONE);
assertEquals(1, jpyMoney.getCent());
}
}