rubyruby-on-rails-3.1multiplicationfloating-point-precision

Ruby - Multiplication issue


My output is like this -

ruby-1.9.2-p290 :011 > 2.32 * 3
=> 6.959999999999999

And I remember sometime back on another machine I had got it like.. 2.32 * 3 = 6

What is my mistake? Thanks a ton for reading this. :)


Solution

  • If you really want to round down to an integer then just

    (3 * 2.32).to_i
    

    but I think that's unlikely. Usually you just want to format the slightly imprecise floating point number to something like this

    "%0.2f" % (3 * 2.32) 
    => "6.96"
    

    If you really want to work with the exact representation then you can use BigDecimal.

    require 'BigDecimal'
    (3 * BigDecimal.new("2.32")).to_s("F")
    => "6.96"
    

    PS. Recommended read http://floating-point-gui.de/ DS.