Module: Footing::Numeric

Defined in:
lib/extensions/numeric.rb

Instance Method Summary collapse

Instance Method Details

#flip_signObject

Flips the sign on the number making it either either positive or negative.



17
18
19
# File 'lib/extensions/numeric.rb', line 17

def flip_sign
  self * -1
end

#negativeObject

Returns a negative representation of the number.



11
12
13
14
# File 'lib/extensions/numeric.rb', line 11

def negative
  return self if self < 0
  flip_sign
end

#percent_of(number) ⇒ Object

Returns the percentage that this number is of the passed number.

Examples:

8.percent_of(10) # => 80.0

Parameters:

  • number (Numeric)

    The number to calculate the percentage with



25
26
27
28
# File 'lib/extensions/numeric.rb', line 25

def percent_of(number)
  percent = (self.to_f / number.to_f) * 100 if number > 0
  percent ||= 0.0
end

#positiveObject

Returns a positive representation of the number.



5
6
7
8
# File 'lib/extensions/numeric.rb', line 5

def positive
  return self if self >= 0
  flip_sign
end

#round_to(decimal_places) ⇒ Object

Rounds the number to a certain number of decimal places.

Examples:

1.784329.round_to(1) # => 1.8

Parameters:

  • decimal_places (Numeric)

    The number of decimal places to round to



34
35
36
# File 'lib/extensions/numeric.rb', line 34

def round_to(decimal_places)
  (self * 10**decimal_places).round.to_f / 10**decimal_places
end