Skip to content

Commit

Permalink
Support exponential notation for Money.parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitrino committed Nov 17, 2022
1 parent d5de842 commit eb4b0d2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/money.ex
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ defmodule Money do
defp prepare_parse_string(["9" | tail], delimiter, acc),
do: prepare_parse_string(tail, delimiter, ["9" | acc])

defp prepare_parse_string(["e" | tail], delimiter, acc),
do: prepare_parse_string(tail, delimiter, ["e" | acc])

defp prepare_parse_string(["E" | tail], delimiter, acc),
do: prepare_parse_string(tail, delimiter, ["E" | acc])

defp prepare_parse_string([delimiter | tail], delimiter, acc),
do: prepare_parse_string(tail, delimiter, ["." | acc])

Expand Down
3 changes: 3 additions & 0 deletions test/money_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ defmodule MoneyTest do
assert Money.parse("$- 1,000.00", :USD) == {:ok, usd(-100_000)}
assert Money.parse("-1000.0", :USD) == {:ok, usd(-100_000)}

assert Money.parse("5.0e3", :USD) == {:ok, usd(500_000)}
assert Money.parse("500e-3", :USD) == {:ok, usd(50)}

assert Money.parse(Decimal.from_float(-1000.0), :USD) == {:ok, usd(-100_000)}
assert Money.parse(Decimal.from_float(4000.765), :USD) == {:ok, usd(4000_77)}

Expand Down

0 comments on commit eb4b0d2

Please sign in to comment.