Class: Numeric

Inherits:
Object show all
Defined in:
lib/utilities/numeric.rb

Instance Method Summary collapse

Instance Method Details

#degreesObject

Convert to degrees



3
4
5
# File 'lib/utilities/numeric.rb', line 3

def degrees
  self * Math::PI / 180
end

#hour_to_string(delimiter = ':') ⇒ Object

Transform self to a string formatted time (HH:MM) Ex: 14.5 => “14:30“



13
14
15
16
17
# File 'lib/utilities/numeric.rb', line 13

def hour_to_string delimiter = ':'
  hour  = self.to_i
  min   = "%02d" % (self.abs * 60 % 60).to_i
  "#{hour}#{delimiter}#{min}"
end

#percentage_of(n, t = 100) ⇒ Object Also known as: percent_of

Calculate the percentage of self on n



36
37
38
# File 'lib/utilities/numeric.rb', line 36

def percentage_of n, t = 100
  n == 0 ? 0.0 : self / n.to_f * t
end

#rank(min, max) ⇒ Object

Calculate the rank of self based on provided min and max



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

def rank min, max
  s, min, max = self.to_f, min.to_f, max.to_f
  min == max ? 0.0 : (s - min) / (max - min)
end

#sqrtObject

Return the square root of self



20
21
22
# File 'lib/utilities/numeric.rb', line 20

def sqrt
  Math.sqrt(self)
end

#squareObject

Return the square of self



8
9
10
# File 'lib/utilities/numeric.rb', line 8

def square
  self * self
end

#to_decimals(decimals = 2) ⇒ Object

Transforms self to a string with decimals decimals



31
32
33
# File 'lib/utilities/numeric.rb', line 31

def to_decimals decimals = 2
  "%.#{decimals}f" % self
end