Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cash rounding for NOK/SEK is partly incorrect #421

Open
soer84 opened this issue Oct 9, 2024 · 0 comments
Open

Cash rounding for NOK/SEK is partly incorrect #421

soer84 opened this issue Oct 9, 2024 · 0 comments

Comments

@soer84
Copy link

soer84 commented Oct 9, 2024

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!

@soer84 soer84 changed the title Cash rounding for NOK is partly incorrect Cash rounding for NOK/SEK is partly incorrect Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant