From: Gary Wright Date: 2009-02-13T15:07:08+09:00 Subject: Re: Bug in converting float to int? On Feb 13, 2009, at 12:41 AM, 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 Ruby floating point values are stored in binary format. 10.12 (decimal) can not be represented exactly in binary: >> sprintf("%0.50f", 10.12) => "10.11999999999999921840299066388979554176330566406250" The default conversion of float to string (e.g., for output) rounds to 6 (or maybe 7) places and truncates trailing zeros: >> sprintf("%0.6f", a).sub(/0+\Z/,'') => "10.12" Float#to_i truncates: >> sprintf("%0.50f", 10.12 * 100 ) => "1011.99999999999988631316227838397026062011718750000000" >> (10.12 * 100).to_i => 1011