Merge pull request #3970 from asukavuuyn/v5-dev

fix(Money): currency scaling bug(币种小数位硬编码)
This commit is contained in:
Golden Looly 2025-06-20 11:06:37 +08:00 committed by GitHub
commit 28b21d7617
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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());
}
}