class Numeric
See the math module, as methods from Math were added in Numeric
dynamically.
Public Instance Methods
crop(range_or_min, max=nil)
click to toggle source
Credit to apeiros for this method.
Min/max method.
Example: -2.crop(0..1) # => 0
Returns: Numeric
# File lib/extra_lib/core_ext/numeric.rb, line 43 def crop(range_or_min, max=nil) range = max ? range_or_min..max : range_or_min if range.include?(self) self elsif self < range.first range.first else range.last end end
format(comma = ',', decimal = '.')
click to toggle source
Add commas every 3 spots in a number.
Example: (4569810.12).format #=> 4,569,810.12
Returns: Commatized string
# File lib/extra_lib/core_ext/numeric.rb, line 31 def format(comma = ',', decimal = '.') to_s.reverse.scan(/(?:-?\d{1,3}(?:\.\d{1,3})?-?)/).map { |s| s.sub('.', decimal) }.join(comma).reverse end
negative?()
click to toggle source
Checks whether a number is a negative number. Example: -4.negative? #=> true
Returns: True or false
# File lib/extra_lib/core_ext/numeric.rb, line 10 def negative? self < 0 end
positive?()
click to toggle source
Checks whether a number is positive. Here we will consider zero as being a positive number.
Example: 5.positive? #=> true
Returns: True or false
# File lib/extra_lib/core_ext/numeric.rb, line 21 def positive? self >= 0 end