JavaMoney / jsr354-ri

JSR 354 - Moneta: Reference Implementation
Other
344 stars 101 forks source link

Cash rounding for NOK/SEK is partly incorrect #421

Open soer84 opened 1 month ago

soer84 commented 1 month ago

Good Day!

TL;DR: When using NOK which rounds to whole Norwegian Krone i.e. rounding 0.499 will result in 1 Krone whereas rounding 0.49 will result in 0 Krone.

I'm not sure if its intended behaviour (and if so feel free to just close this ticket again)

More detailled description:

When using

BigDecimal amount = new BigDecimal("0.499");
MonetaryRounding rounding = Monetary.getRounding(RoundingQueryBuilder.of().setCurrency(Monetary.getCurrency("NOK")).set("cashRounding", cashRounding).build());

Money.of(amount, currencyCode).abs().with(rounding).multiply(amount.signum()).getNumber().numberValueExact(BigDecimal.class);

The value returned by "numberValueExact" will change depending on the number set on the third place. So for NOK (and also SEK) the rounding from point of view is not correct as 0,49 and 0,499 should be treated equally and be rounded down to 0.

The issue here is located in DefaultCashRounding (https://github.com/JavaMoney/jsr354-ri/blob/master/moneta-core/src/main/java/org/javamoney/moneta/spi/DefaultCashRounding) which first rounds the given amount to a certain precisicion and will then round a second time to the smallest available unit.

For NOK that means: 0,49 will stay 0,49 the smallest Unit for Krone is one Krone so 0,49 gets rounded down to 0

For the amount 0,499 however 0,49 will be rounded to 0,5 the smallest Unit for Krone is one Krone so 0,5 gets rounded up to 1

If that is not intended behavior I can provide a suggestion via Merge Request - just let me know.

Thanks in advance!