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

Open
nathany-copia opened this issue Mar 7, 2024 · 0 comments
Open

Money.parse doesn't handle scientific notation #216

nathany-copia opened this issue Mar 7, 2024 · 0 comments

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?

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