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

fix: avoid float rounding in Money.to_string #139

Merged

Conversation

polvalente
Copy link
Contributor

Big numbers like 123_456_789_123_456_789_123_456_789_123_456_789 suffer from float rounding errors due to the way the conversion in Money.to_string is implemented.

This PR aims to avoid such problems by performing the initial conversion and split using Integer and String functions only.

The following example (included in the new assertions I added) shows the bug.

iex(1)> 123_456_789_123_456_789_123_456_789_123_456_789 |> Money.new(:USD) |> Money.to_string
"$1,234,567,891,234,567,846,791,203,981,885,440.00"
iex(2)> Money.new(123_456_789_123_456_789_123_456_789_123_456_789, :USD) |> Money.to_string == "$1,234,567,891,234,567,891,234,567,891,234,567.89"
false

@Nitrino
Copy link
Member

Nitrino commented Apr 20, 2020

@polvalente Great work. Thanks! 🔥

@Nitrino Nitrino merged commit cc14c6b into elixirmoney:master Apr 20, 2020
@polvalente polvalente deleted the fix/avoid-float-rounding-to-string branch April 20, 2020 19:26
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

Successfully merging this pull request may close these issues.

None yet

2 participants