Module: EggCarton::Helper

Defined in:
lib/egg_carton/helper.rb

Instance Method Summary collapse

Instance Method Details

#average(numerator, denominator, precision = 0) ⇒ Object

Public: Calculates average for a numerator and denominator.

numberator - The Integer or Float to use as the numberator. denominator - The Integer or Float to use as the denominator. precision - The Integer or decimal places to preserve.

Examples

average(1, 4, 2)
# => 0.25

average(1, 4, 1)
# => 0.3

Returns the Float result of the calculation



20
21
22
23
# File 'lib/egg_carton/helper.rb', line 20

def average(numerator, denominator, precision = 0)
  return 0 if denominator.zero?
  (numerator.to_f / denominator.to_f).round(precision)
end

#conversion(numerator, denominator, precision = 0, opts = {}) ⇒ Object

Public: Calculates conversion for a numerator and denominator.

numberator - The Integer or Float to use as the numberator. denominator - The Integer or Float to use as the denominator. precision - The Integer or decimal places to preserve. opts - The options Hash

Examples

conversion(1, 4, 2)
# => "25%"

conversion(1, 4, 2, :percentage => false)
# => "0.25"

conversion(1, 4, 1)
# => "30%"

conversion(1, 4, 1, :percentage => false)
# => "0.3"

Returns the Float result of the calculation



47
48
49
50
51
52
53
# File 'lib/egg_carton/helper.rb', line 47

def conversion(numerator, denominator, precision = 0, opts={})
  return 0 if denominator.zero?
  as_percentage = opts[:percentage].nil? ? true : opts[:percentage]

  result = (numerator.to_f / denominator.to_f * 100).round(precision)
  as_percentage ? "#{result}%" : "#{result}"
end