Class: Numeric

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

Instance Method Summary collapse

Instance Method Details

#clamp(min, max) ⇒ Object

Return a clamped value between a minimum and maximum value



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

def clamp min, max
  [min, self, max].sort[1]
end

#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“



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

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



41
42
43
# File 'lib/utilities/numeric.rb', line 41

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



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

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



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

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



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

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