From: Florian Gross Date: 2004-10-28T04:24:02+09:00 Subject: Re: Rounding error, (100.0 * 9.95).to_i == 994 Dan Janowski wrote: > I would agree if it were not for this: > > $ ruby -e 'puts (100.0*9.95).to_s' > 995 > $ ruby -e 'puts (100.0*9.95).to_i.to_s' > 994 > > Ruby seems to be able to manage the FP error rate since FP.to_s show the > exact value. Why shouldn't to_i benefit? The value is even less exact. .to_s automatically rounds to 15 digits. Internally a higher precision is used. (Though it is still not perfect.) Basically Ruby 'blurs' the values on output so that you need not bother about manually rounding them all the time. Your problem is that you're using .to_i. That will give you 5.0 even for values like 5.99999999999999. I hope my explanation was of some help.