class Percentage

Public Class Methods

from_amount(val = 0, options = {}) click to toggle source
# File lib/percentage.rb, line 120
def self.from_amount(val = 0, options = {})
  val = val.to_r  if val.is_a?(String) && val['/']
  val = val.to_d  if val.is_a?(String)
  val = val / 100 if val.is_a?(Numeric) && !val.integer?

  self.new val, options
end
from_fraction(val = 0, options = {}) click to toggle source

Additional initialization methods

# File lib/percentage.rb, line 113
def self.from_fraction(val = 0, options = {})
  val = val.to_i if val.is_a?(String) && !(val['/'] || val['%'] || val['.'])
  val = val.to_d if val.is_a?(Integer)

  self.new val, options
end
new(val = 0, options = {}) click to toggle source
# File lib/percentage.rb, line 5
def initialize(val = 0, options = {})
  val = 0.0            if !val
  val = 1.0            if val == true
  val = val.to_r       if val.is_a?(String) && val['/']
  val = val.to_f       if val.is_a?(Complex) || val.is_a?(Rational)
  val = val.to_i       if val.is_a?(String) && !val['.']
  val = val.to_d / 100 if val.is_a?(Integer) || (val.is_a?(String) && val['%'])
  val = val.value      if val.is_a? self.class

  @value = val.to_d
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/percentage.rb, line 91
def <=> other
  self.to_f <=> other.to_f
end
==(other) click to toggle source

Comparisons

# File lib/percentage.rb, line 83
def == other
  self.eql?(other) || self.to_f == other
end
eql?(other) click to toggle source
# File lib/percentage.rb, line 87
def eql? other
  self.class == other.class && self.value == other.value
end
format(options = {}) click to toggle source
# File lib/percentage.rb, line 48
def format(options = {})
  # set defaults; all other options default to false
  options[:percent_sign] = options.fetch :percent_sign, true

  if options[:as_decimal]
    return self.to_str
  elsif options[:rounded]
    string = self.to_float.round.to_s
  elsif options[:no_decimal]
    string = self.to_i.to_s
  elsif options[:no_decimal_if_whole]
    string = self.to_s
  else
    string = self.to_float.to_s
  end

  string += ' ' if options[:space_before_sign]
  string += '%' if options[:percent_sign]
  return string
end
inspect() click to toggle source
# File lib/percentage.rb, line 76
def inspect
  "#<#{self.class.name}:#{self.object_id}, #{self.value.inspect}>"
end
to_amount() click to toggle source

Additional conversion methods

# File lib/percentage.rb, line 72
def to_amount
  (int = self.to_i) == (float = self.to_float) ? int : float
end
to_c() click to toggle source
# File lib/percentage.rb, line 28
def to_c; self.value.to_c; end
to_complex() click to toggle source

Convert percentage fraction to percent amount

# File lib/percentage.rb, line 36
def to_complex;  (self.value * 100).to_c; end
to_d() click to toggle source
# File lib/percentage.rb, line 29
def to_d; self.value.to_d; end
to_decimal() click to toggle source
# File lib/percentage.rb, line 37
def to_decimal;  (self.value * 100).to_d; end
to_f() click to toggle source
# File lib/percentage.rb, line 30
def to_f; self.value.to_f; end
to_float() click to toggle source
# File lib/percentage.rb, line 38
def to_float;    (self.value * 100).to_f; end
to_i() click to toggle source

Convert percentage to different number formats

# File lib/percentage.rb, line 27
def to_i; (self.value * 100).to_i; end
to_r() click to toggle source
# File lib/percentage.rb, line 31
def to_r; self.value.to_r; end
to_rational() click to toggle source
# File lib/percentage.rb, line 39
def to_rational; (self.value * 100).to_r; end
to_s() click to toggle source

String conversion methods

# File lib/percentage.rb, line 44
def to_s;      self.to_amount.to_s; end
to_str() click to toggle source
# File lib/percentage.rb, line 45
def to_str;    self.to_f.to_s;      end
to_string() click to toggle source
# File lib/percentage.rb, line 46
def to_string; self.to_s + '%';     end
value() click to toggle source

Attributes

# File lib/percentage.rb, line 20
def value
  @value ||= 0
end