Search code examples
ruby-on-railsrubybigdecimal

Ruby 2.4 and BigDecimal error (invalid value)


Given a value in a hash like this:

billed_amt:""

This code then:

BigDecimal.new(tt[:billed_amt].to_s)

results in this error:

ArgumentError: invalid value for BigDecimal(): ""

Is this correct behaviour? I have tried updating the gem with no luck.


Solution

  • This is correct behaviour. You can read about it in "BigDecimal fix for Rails 4 with Ruby 2.4".

    Big Decimal 1.3.0, which ships with Ruby 2.4, was changed to throw an exception on invalid values to the constructor rather than returning 0 as it did in Ruby 2.3. This was done so that it was more consistent with the other numeric types (Integer, Float, e.t.c.).

    There have been suggestions to revert this functionality however it is likely it won't happen as BigDecimal needs to be consistent with the other numeric types.