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

Unexpected rounding behaviour using FastMoney #384

Closed
devmop opened this issue Feb 16, 2023 · 1 comment
Closed

Unexpected rounding behaviour using FastMoney #384

devmop opened this issue Feb 16, 2023 · 1 comment

Comments

@devmop
Copy link

devmop commented Feb 16, 2023

Apologies for the long test case attached.

We've been exploring using JavaMoney for some internal currency conversions using an internal exchange rate provider.
Unit testing has thrown up some undesirable interactions which I believe I've tracked down and identified workarounds but it seems worthy of a report.

The attached tests attempt to convert 100 GBP to JPY with a rate of 175 expressed as range of Number implementations.
All tests except for FastMoney with Float and Double pass as expected. Reporting a total JPY of 17500.
Float and Double however generate 20000 JPY which is an incredible amount of error.

The cause appears to be in org.javamoney.moneta.spi.AbstractCurrencyConversion#roundFactor
When using Money no scaling is applied as the underlying value is BigDecimal and the supplied exchange rate is applied directly.

For FastMoney it takes the main branch, finds the scale and attempts to round.
For all cases except floats/doubles the scale is zero by virtue of the attempts to strip trailing zeros, and the MathContext then is asked for precision 0 and applies no rounding.
Double/Float however get scale 1 and the MathContext is asked for precision 1. This returns a result with a single significant figure of 200 in the case of 175 in the tests.

I believe this is also the same errors as were seen in #303

FastMoneyRoundingError.txt

@keilw
Copy link
Member

keilw commented Feb 24, 2023

This was probably fixed already, but if it still happens with the 1.4.3-SNAPSHOT builds, could you please provide the text file for TestNG instead of JUnit 5? (all tests in the RI are TestNG and we can't change that with the constrained resources in maintenance mode of the JSR)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants