Module: Footing::Numeric
- Defined in:
- lib/extensions/numeric.rb
Instance Method Summary collapse
-
#flip_sign ⇒ Object
Flips the sign on the number making it either either positive or negative.
-
#negative ⇒ Object
Returns a negative representation of the number.
-
#percent_of(number) ⇒ Object
Returns the percentage that this number is of the passed number.
-
#positive ⇒ Object
Returns a positive representation of the number.
-
#round_to(decimal_places) ⇒ Object
Rounds the number to a certain number of decimal places.
Instance Method Details
#flip_sign ⇒ Object
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 |
#negative ⇒ Object
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.
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 |
#positive ⇒ Object
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.
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 |