java MonetaryConversions 为高位货币抛出 ArithmeticException
java MonetaryConversions throws ArithmeticException for high digit currencies
我想使用标准 java MonetaryConversions 来转换货币。
乍一看它很好用而且简单:
@Test
public void testConversion()
{
FastMoney usd = FastMoney.of(1000, Monetary.getCurrency("USD"));
usd.with(MonetaryConversions.getConversion("EUR"));
}
然而,当我使用具有高面值的货币(如日元或墨西哥比索)时,我发现它会抛出 ArithmeticExceptions
@Test
public void testArithmeticException()
{
FastMoney jpy = FastMoney.of(1000, Monetary.getCurrency("JPY"));
jpy.with(MonetaryConversions.getConversion("EUR"));
}
抛出以下异常
java.lang.ArithmeticException: 0.0082769 can not be represented by this class, scale > 5
at org.javamoney.moneta.FastMoney.getInternalNumber(FastMoney.java:197)
at org.javamoney.moneta.FastMoney.multiply(FastMoney.java:388)
at org.javamoney.moneta.FastMoney.multiply(FastMoney.java:84)
at org.javamoney.moneta.spi.AbstractCurrencyConversion.apply(AbstractCurrencyConversion.java:118)
at org.javamoney.moneta.FastMoney.with(FastMoney.java:594)
at tech....GatewayTransactionConverterTest.testArithmeticException(GatewayTransactionConverterTest.java:207)
检查 FastMoney 的代码,我发现异常是硬编码的,我找不到任何可以减少的地方,例如规模。
但是 java 提供的开箱即用的转换非常无用,因为我无法转换很多货币。我无法想象没有人有这个问题。但是我找不到 google.
的任何内容
作为
Can you use the bigdecimal-based Money class?The FastMoney class is based on longs, which you shouldn't rely on for high precision currency calculations.
这对我有用。
我想使用标准 java MonetaryConversions 来转换货币。
乍一看它很好用而且简单:
@Test
public void testConversion()
{
FastMoney usd = FastMoney.of(1000, Monetary.getCurrency("USD"));
usd.with(MonetaryConversions.getConversion("EUR"));
}
然而,当我使用具有高面值的货币(如日元或墨西哥比索)时,我发现它会抛出 ArithmeticExceptions
@Test
public void testArithmeticException()
{
FastMoney jpy = FastMoney.of(1000, Monetary.getCurrency("JPY"));
jpy.with(MonetaryConversions.getConversion("EUR"));
}
抛出以下异常
java.lang.ArithmeticException: 0.0082769 can not be represented by this class, scale > 5
at org.javamoney.moneta.FastMoney.getInternalNumber(FastMoney.java:197)
at org.javamoney.moneta.FastMoney.multiply(FastMoney.java:388)
at org.javamoney.moneta.FastMoney.multiply(FastMoney.java:84)
at org.javamoney.moneta.spi.AbstractCurrencyConversion.apply(AbstractCurrencyConversion.java:118)
at org.javamoney.moneta.FastMoney.with(FastMoney.java:594)
at tech....GatewayTransactionConverterTest.testArithmeticException(GatewayTransactionConverterTest.java:207)
检查 FastMoney 的代码,我发现异常是硬编码的,我找不到任何可以减少的地方,例如规模。
但是 java 提供的开箱即用的转换非常无用,因为我无法转换很多货币。我无法想象没有人有这个问题。但是我找不到 google.
的任何内容作为
Can you use the bigdecimal-based Money class?The FastMoney class is based on longs, which you shouldn't rely on for high precision currency calculations.
这对我有用。