• July 23, 2014
  • 1 min read

Weirdness of Bigdecimal.new

Recently i was bitten by this weirdness of BigDecimal in Ruby.

BigDecimal.new excepts initial value and number of significant digits. In one of my app, BigDecimal.new was called with string initial value without precision. Everything was working great and suddenly something changed. And this error started coming.

can't omit precision for a Float.

What happened was, the value passed to BigDecimal.new was no longer string. Due to some changes in other parts of code, float was passed to BigDecimal.new.

Now when a float or rational is passed to BigDecimal.new, it requires precision to be present. Otherwise raises error.

can't omit precision for a Float(/Rational).

Fix was very simple. Pass the precision digits as second argument to the BigDecimal.new call.

Leave a comment