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

Money.parse doesn't handle scientific notation #216

Closed
nathany-copia opened this issue Mar 7, 2024 · 3 comments · Fixed by #240
Closed

Money.parse doesn't handle scientific notation #216

nathany-copia opened this issue Mar 7, 2024 · 3 comments · Fixed by #240

Comments

@nathany-copia
Copy link

nathany-copia commented Mar 7, 2024

I consider this a bug in our code, but I wanted to document it here, just so people are aware.

We had some code where Decimals were being represented in scientific notation, e.g.

value = Decimal.new("0")
divisor = :math.pow(10, 2) |> Decimal.from_float() # Decimal.new("100.0")
value = Decimal.div(value, divisor) # Decimal.new("0E+1")

That itself isn't a problem. The problem happens with some Money.parse code like this:

Money.parse!("#{value}", currency)

Which is effectively doing this:

Money.parse!("0E+1", :USD) # %Money{amount: 100, currency: :USD}

So converting to a string before parsing Money is resulting in $1 when the original input was 0, or more specifically 0E+1.

Passing the Decimal directly to Money works just fine.

Decimal.new("0E+1") |> Money.parse!(:USD) # %Money{amount: 0, currency: :USD}

I'm not sure if this should be "fixed" -- is Money supposed to be able to parse scientific notation? Maybe it just needs to be called out as not supported?

@Nitrino
Copy link
Member

Nitrino commented Feb 26, 2025

@nathany-copia Sorry for the extremely long reply, I haven't been able to develop the library for a long time. This is probably no longer relevant to you, but I've added support for parsing scientific notation

@nathany-copia
Copy link
Author

thanks @Nitrino

Will there be a hex release?

@Nitrino
Copy link
Member

Nitrino commented Mar 10, 2025

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 a pull request may close this issue.

2 participants