Ruby strange issue with floating point multiplication

ruby, ruby-on-rails

Solution

Whenever you get funky numbers in calculations use bigdecimal

require 'bigdecimal'
a = BigDecimal(8.1999999.to_s)
(a.round(2) * 1000000).to_i

Problem

Does anybody has solution for this problem in ruby : let say we have : a = 8.1999999 We wanted to round it by 2 decimal which is 8.20 and multiply it by 1,000,000 to become 8,200,000 We do it this way ; ``` (a.round(2) * 1000000).to_i ``` But what we got is 8199999, why? The strage things is, we got the correct result if we multiply by 1000, 100000, or 10000000, but not 1000000. Any body know why? We are using ruby 1.9.2 and try with 1.9.3 as well. Thanks!

Original source

Related problems