From: Colin Bartlett Date: 2010-01-02T21:08:02+09:00 Subject: Re: Rounding float in ruby via .round On Sat, Jan 2, 2010 at 2:13 AM, Phillip Gawlowski wrote: > Hmmm.. > irb(main):001:0> RUBY_VERSION #=> "1.9.1" > irb(main):002:0> 3.14567534.round #=> 3 > irb(main):003:0> 3.14567534.round 3 #=> 3.146 I didn't know 1.9 had extended Float#round. That's useful. Thank you for the information. But I'm not sure that Float#round should be allowed to have non-integer arguments: # IRB Ruby 1.9.1 n = 9876.54321 f = 1.75 r = Rational(7, 4) #=> (7/4) c = Complex(f, 0) #=> (1.75+0i) ci = Complex(f, 2) #=> (1.75+2i) n.round #=> 9877 n.round 0 #=> 9877 n.round -1 #=> 9880 n.round 1 #=> 9876.5 n.round f #=> 9876.5 n.round r #=> 9876.5 n.round c #=> 9876.5 n.round ci # RangeError: can't convert 1.75+2i into Integer # it's nice that Integer#round is consistent with Float#round i = 9876 i.round #=> 9876 i.round 0 #=> 9876 i.round -1 #=> 9880 i.round 1 #=> 9876.0 # Rational#round: an opportunity for someone? r = 9876 + Rational( 54321, 100000 ) #=> (987654321/100000) r.to_f #=> 9876.54321 r.round #=> 9877 r.round 1 # ArgumentError: wrong number of arguments(1 for 0)