From: Joao Silva Date: 2012-10-19T08:05:15+09:00 Subject: Re: Truncate number which ends with 0 decimal Wybo Dekker wrote in post #1080338: > On 2012-10-18 22:34, Joao Silva wrote: >> Hi All. >> >> The question is how I can do to truncate a float number ending in 0. > > $ irb >>> class Numeric; def to_2; "%.2f" % self end end > => nil >>> 2345.5000.to_2 > => "2345.50" >>> 2.to_2 > => "2.00" This code works fine when the decimal ends in zero, but if the decimal number ends in another, round it, and that's not what I want. class Numeric def to_2 "%.2f" % self end end 2345.5000.to_2 ==>> 2345.50 is ok. 2345.5876374536 ==>> 2345.59 ==>> is wrong ==>> should be 2345.58 1234.567889 ==>> 1234.57 ==>> is wrong ==>> should be 1234.56 2659876.76789999 ==>> 2659876.77 ==> is wrong ==>> should be 2659876.76 -- Posted via http://www.ruby-forum.com/.