Sha256: 21a66cefe8e687f3ca39b441d9b31a40d5f5f2cc2962bedd6ce47a615ba528e2

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

module Spruz
  # A bit more versatile rounding for Ruby
  module Round
    def self.included(klass)
      if klass.instance_method(:round)
        klass.class_eval do
          begin
            alias_method :__old_round__, :round
            remove_method :round
          rescue NameError
          end
        end
        super
      else
        raise NoMethodError, 'no round method found'
      end
    end

    def round(places = nil)
      if places == nil || places == 0
        return __old_round__
      elsif places.respond_to?(:to_int)
        places = places.to_int
      else
        raise TypeError, "argument places has to be like an Integer"
      end
      if places < 0
        max_places = -Math.log(self.abs + 1) / Math.log(10)
        raise ArgumentError, "places has to be >= #{max_places.ceil}" if max_places > places
      end
      t = self
      f = 10.0 ** places
      t *= f
      if t.infinite?
        result = self
      else
        if t >= 0.0
          t = (t + 0.5).floor
        elsif t < 0.0
          t = (t - 0.5).ceil
        end
        t /= f
        result = t.nan? ? self : t
      end
      max_places and result = result.to_i # if places < 0
      result
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spruz-0.2.13 lib/spruz/round.rb
spruz-0.2.12 lib/spruz/round.rb