kipcole9 / money

Elixir implementation of Money with Currency

Home Page:https://hex.pm/packages/ex_money

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"NaN" intentionally a supported Money amount?

coladarci opened this issue · comments

I was surprised to learn today that this works:

iex > Money.new(:USD, "NaN")
Money.new(:USD, "NaN")

I would sort of have expected an error similar to:

iex > Money.new(:USD, "greg")
{:error,
 {Money.InvalidAmountError, "Amount cannot be converted to a number: \"greg\""}}

We have some legacy data floating around that, it turns out have been persisting these due to bad data. We then tried to call Money.to_integer_exp(money) on one of them and that raises:

(We are on Money 5.10.0 at the moment so the stack trace doesn't line up w/ master)

ArithmeticError: bad argument in arithmetic expression
  Module "erlang", in :erlang.*/2
  File "lib/money.ex", line 1947, in Money.to_integer_exp/2

Curious your thoughts on this; we can figure out a work around on our end pretty easily so we aren't blocked here.

Thanks, again, for everything you do!

This is probably a sideeffect of the underlying Decimal library, which supports :NaN as well as :inf. Not sure if that's an intended sideeffect or not though.

@coladarci thanks for the report and definitely a bug (arises for the reasons @LostKobrakai noted). I have published ex_money version 5.12.2 with the following changelog entry:

Bug Fixes

  • Don't create "NaN" or "Inf" valued Money structs. Thanks for @coladarci for the report. Closes #143.