Attempting to create a Joda-Money Money
object from a BigDecimal read from a MySQL database throws an error.
This code:
PreparedStatement p_stmt = ...;
ResultSet results = ...;
Money amount = Money.of(CurrencyUnit.USD, results.getBigDecimal("amount"));
Throws this:
java.lang.ArithmeticException: Scale of amount 1.0000 is greater than the scale of the currency USD
at org.joda.money.Money.of(Money.java:74)
at core.DB.getMoney(DB.java:4821)
I have actually solved the error by adding rounding:
Money amount = Money.of(CurrencyUnit.USD, results.getBigDecimal("amount"), RoundingMode.HALF_UP);
But, I am leery of this solution. Is it the best?.
I am using DECIMAL(19,4)
to store the money values on the DB as per this SO answer. Honestly, it kind of confused me why the answerer there called for 4 decimal place precision on the DB, but I tend to trust high-valued answers and I assume they know what they're talking about and that I'll regret not following their advice. Yet Joda-Money does not like 4 decimal place precision with US Currency. Maybe DECIMAL(19,4)
was an international standard where they need 4 decimal place precision? Not sure..
To make the questions succinct:
- Is
RoundingMode.HALF_UP
the ideal solution to solve this error? - Should I change the precision on the MySQL DB from
DECIMAL(19,4)
toDECIMAL(19,2)
? - Is there a way to change the precision in Joda-Money? If yes: should I?