Sha256: 76431cb0638555d62849d065e336ac4cd099be969256bbb3f9cfa081efefae7a
Contents?: true
Size: 1.55 KB
Versions: 1
Compression:
Stored size: 1.55 KB
Contents
module EggCarton module Helper # 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 def average(numerator, denominator, precision = 0) return 0 if denominator.zero? (numerator.to_f / denominator.to_f).round(precision) end # 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 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 end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
egg_carton-0.2 | lib/egg_carton/helper.rb |