Joda Money and BigDecimal defines a 'scale' for each currency type.
The scale is the no. of decimal places the currency uses.
So USD has a scale of two (two decimals after the decimal place).
If you try to instantiate an Money instance for a USD currency from a source that has more than two decimal places then you will get a scale error. e.g.
20.99 is fine
20.991 throws an error.
Other currencies allow different numbers of decimal places.
According to the documentation RoundMode.UNNECESSARY
will actually throw an exception if rounding is actually required.
e.g: Assuming USD which has a scale of 2 then 2 decimal places are expected.
Using:
Money money = Money.of(CurrencyUnit.USD, value, RoundingMode.UNNECESSARY);
value:
1.20 - works fine
1.200 - works fine as whilst extra digit rounding isn't necessary.
1.201 - throws an exception as rounding is necessary
Rounding and money are always a problem. My approach is to store the correct scale in the db
e.g.: if USD then DECIMAL(19,2)
Use RoundingMode.HALF_UP
.
Be careful when doing multiplication and division. Doing multiplication before division will result in less rounding issues. If you round at the time you do the arithmetic you should be OK.