sprintf, floats and rounding

So I had some fun figuring out why my Spec tests were failing today.

tl;dr – do not use floats for currency (BigDecimal will sort you out in Ruby)

I was starting to question my highschool math today as I was seeing things like this:

php > echo sprintf("%0.2f\n", 1.045);


irb(main):003:0> '%0.2f' % 1.045
=> "1.04"

now for those playing at home, this needs to be 1.05 – and since both Ruby and PHP where doing the same, it pointed to an underlying library problem.

A Quick Google later and I found what I needed. 1.045 would be stored in floating point form as 1.04999999999999 – therefore – rounding would be busted.

In Ruby I am using BigDecimal to solve my problem and therefore make my spec tests pass!