Closed l-ray closed 1 year ago
Not sure, which version of Moneta you used for this, but after #357 has been addressed, the assertion would fail:
BigDecimal amount = BigDecimal.valueOf(1000,2);
Money money = Money.of(amount , "EUR");
assertEquals(money.toString(), "EUR 10.00");
java.lang.AssertionError:
Expected :EUR 10.00
Actual :EUR 10
And you may tweak your Money
BigDecimal amount = BigDecimal.valueOf(1000,2);
Money money = Money.of(amount , "EUR", MonetaryContextBuilder.of().setMaxScale(2).setFixedScale(true).build());
assertEquals(money.toString(), "EUR 10.00");
To set the desired scale, otherwise it takes the scale of the BigDecimal
which was 0 in the initial example.
When a MonetaryAmount is instantiated with a scaled BigDecimal, then extracting the big decimal using
Money#getNumber
leads to a BigDecimal with unexpected scale (e.g. "-1").Example
... leads to the following test-failure ...
Background
Using Hibernate with the Jadira multi column user type (or any other Hibernate user type I guess) leads to creation of BigDecimal with a given scale. When on the other end the service returns e.g. BigDecimal - this leads to false service responses.
Workaround
Creating a new BigDecimal with the currencies scale solves the issue.