From: Cai Li Date: 2004-06-28T01:04:15+09:00 Subject: Re: rounding to n decimal digits David Garamond wrote: > I often do this: > > class Float > def round_n(n=0) > raise FloatDomainError unless self.finite? > raise ArgumentError unless n.class == Fixnum && n >= 0 > sprintf("%.#{n}f", self).to_f > end > > def ceil_n(n=0) > raise FloatDomainError unless self.finite? > raise ArgumentError unless n.class == Fixnum && n >= 0 > (self*10**n).ceil.to_f / 10**n > end > > def floor_n(n=0) > raise FloatDomainError unless self.finite? > raise ArgumentError unless n.class == Fixnum && n >= 0 > (self*10**n).floor.to_f / 10**n > end > end > > However I can't help get the feeling that Ruby already has something > like this. Did I indeed miss it? > What I'm looking for in Ruby is a command causing all arithmetic to be rounded to special digits. Like we do in Matlab: >Digits:=t;