From: Raphael Clancy Date: 2009-02-14T05:00:39+09:00 Subject: Re: Bug in converting float to int? Jeff Vogt wrote: > I just tried this on 1.8.6. Is this a bug or am I missing something? > >>> a=10.12 > => 10.12 >>> (a*100).to_i > => 1011 > > And if this is in fact intended behavior, how can I actually get the > results I want? (Converting dollars and cents into cents) Just to tack on my 1.9997438 cents ;) We had a big go-round about this a while back. This behavior is not specific to Ruby. The reason Ruby behaves like this is that it adheres to the IEEE standard for floating point numbers, which is common to most programming languages. A standard so old that it was first pressed into clay tablets back when computers were powered by oxen and water wheels. Ruby keeps this standard because it's fast and it reflects the way that computers really work with numbers. There are plenty of alternatives for dealing with floating point numbers which are usually offered as high precision scientific numerical packages. But these are almost always slower than the IEEE standard. And, while they are less lousy (I imagine most could keep two decimal places straight) they are still prone to precision error, just because computers are, at their very core, integer only. -- Posted via http://www.ruby-forum.com/.