Module: Spruz::Round

### Home Classes Methods

### In Files

- lib/spruz/round.rb
- lib/spruz/xt/round.rb

### Methods

- ::included
- #round

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Spruz
- Spruz::Attempt
- Spruz::Bijection
- Spruz::Blank
- Spruz::Blank::Array
- Spruz::Blank::FalseClass
- Spruz::Blank::Hash
- Spruz::Blank::NilClass
- Spruz::Blank::Numeric
- Spruz::Blank::Object
- Spruz::Blank::String
- Spruz::Blank::TrueClass
- Spruz::CountBy
- Spruz::DeepDup
- Spruz::FileBinary
- Spruz::FileBinary::ClassMethods
- Spruz::FileBinary::Constants
- Spruz::Full
- Spruz::GO
- Spruz::Generator
- Spruz::HashSymbolizeKeysRecursive
- Spruz::HashUnion
- Spruz::Limited
- Spruz::LinesFile
- Spruz::LinesFile::LineExtension
- Spruz::Memoize
- Spruz::Minimize
- Spruz::ModuleGroup
- Spruz::Null
- Spruz::Once
- Spruz::P
- Spruz::PartialApplication
- Spruz::RangePlus
- Spruz::Round
- Spruz::SecureWrite
- Spruz::Shuffle
- Spruz::StringCamelize
- Spruz::StringUnderscore
- Spruz::StringVersion
- Spruz::StringVersion::Version
- Spruz::Subhash
- Spruz::TimeDummy
- Spruz::TimeDummy::ClassMethods
- Spruz::ToProc
- Spruz::UniqBy
- Spruz::Write
- Array
- Enumerable
- File
- Float
- Hash
- IO
- IRB
- Integer
- Method
- Module
- Object
- Proc
- Range
- String
- Symbol
- Time

No matching classes.

# Spruz::Round

A bit more versatile rounding for Ruby

### Public Class Methods

included(klass) click to toggle source

    # File lib/spruz/round.rb, line 4
    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

### Public Instance Methods

round(places = nil) click to toggle source

    # File lib/spruz/round.rb, line 19
    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

[Validate]

Generated with the Darkfish Rdoc Generator 2.