Class: Percentage
- Inherits:
-
Numeric
- Object
- Numeric
- Percentage
- Defined in:
- lib/percentage.rb
Class Method Summary collapse
- .from_amount(val = 0, options = {}) ⇒ Object
-
.from_fraction(val = 0, options = {}) ⇒ Object
Additional initialization methods.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#==(other) ⇒ Object
Comparisons.
- #eql?(other) ⇒ Boolean
- #format(options = {}) ⇒ Object
-
#initialize(val = 0, options = {}) ⇒ Percentage
constructor
A new instance of Percentage.
- #inspect ⇒ Object
-
#to_amount ⇒ Object
Additional conversion methods.
- #to_c ⇒ Object
-
#to_complex ⇒ Object
Convert percentage fraction to percent amount.
- #to_d ⇒ Object
- #to_decimal ⇒ Object
- #to_f ⇒ Object
- #to_float ⇒ Object
-
#to_i ⇒ Object
Convert percentage to different number formats.
- #to_r ⇒ Object
- #to_rational ⇒ Object
-
#to_s ⇒ Object
String conversion methods.
- #to_str ⇒ Object
- #to_string ⇒ Object
-
#value ⇒ Object
Attributes.
Constructor Details
#initialize(val = 0, options = {}) ⇒ Percentage
Returns a new instance of Percentage.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/percentage.rb', line 5 def initialize(val = 0, = {}) 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 |
Class Method Details
.from_amount(val = 0, options = {}) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/percentage.rb', line 120 def self.from_amount(val = 0, = {}) 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, end |
.from_fraction(val = 0, options = {}) ⇒ Object
Additional initialization methods
113 114 115 116 117 118 |
# File 'lib/percentage.rb', line 113 def self.from_fraction(val = 0, = {}) val = val.to_i if val.is_a?(String) && !(val['/'] || val['%'] || val['.']) val = val.to_d if val.is_a?(Integer) self.new val, end |
Instance Method Details
#<=>(other) ⇒ Object
91 92 93 |
# File 'lib/percentage.rb', line 91 def <=> other self.to_f <=> other.to_f end |
#==(other) ⇒ Object
Comparisons
83 84 85 |
# File 'lib/percentage.rb', line 83 def == other self.eql?(other) || self.to_f == other end |
#eql?(other) ⇒ Boolean
87 88 89 |
# File 'lib/percentage.rb', line 87 def eql? other self.class == other.class && self.value == other.value end |
#format(options = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/percentage.rb', line 48 def format( = {}) # set defaults; all other options default to false [:percent_sign] = .fetch :percent_sign, true if [:as_decimal] return self.to_str elsif [:rounded] string = self.to_float.round.to_s elsif [:no_decimal] string = self.to_i.to_s elsif [:no_decimal_if_whole] string = self.to_s else string = self.to_float.to_s end string += ' ' if [:space_before_sign] string += '%' if [:percent_sign] return string end |
#inspect ⇒ Object
76 77 78 |
# File 'lib/percentage.rb', line 76 def inspect "#<#{self.class.name}:#{self.object_id}, #{self.value.inspect}>" end |
#to_amount ⇒ Object
Additional conversion methods
72 73 74 |
# File 'lib/percentage.rb', line 72 def to_amount (int = self.to_i) == (float = self.to_float) ? int : float end |
#to_c ⇒ Object
28 |
# File 'lib/percentage.rb', line 28 def to_c; self.value.to_c; end |
#to_complex ⇒ Object
Convert percentage fraction to percent amount
36 |
# File 'lib/percentage.rb', line 36 def to_complex; (self.value * 100).to_c; end |
#to_d ⇒ Object
29 |
# File 'lib/percentage.rb', line 29 def to_d; self.value.to_d; end |
#to_decimal ⇒ Object
37 |
# File 'lib/percentage.rb', line 37 def to_decimal; (self.value * 100).to_d; end |
#to_f ⇒ Object
30 |
# File 'lib/percentage.rb', line 30 def to_f; self.value.to_f; end |
#to_float ⇒ Object
38 |
# File 'lib/percentage.rb', line 38 def to_float; (self.value * 100).to_f; end |
#to_i ⇒ Object
Convert percentage to different number formats
27 |
# File 'lib/percentage.rb', line 27 def to_i; (self.value * 100).to_i; end |
#to_r ⇒ Object
31 |
# File 'lib/percentage.rb', line 31 def to_r; self.value.to_r; end |
#to_rational ⇒ Object
39 |
# File 'lib/percentage.rb', line 39 def to_rational; (self.value * 100).to_r; end |
#to_s ⇒ Object
String conversion methods
44 |
# File 'lib/percentage.rb', line 44 def to_s; self.to_amount.to_s; end |
#to_str ⇒ Object
45 |
# File 'lib/percentage.rb', line 45 def to_str; self.to_f.to_s; end |
#to_string ⇒ Object
46 |
# File 'lib/percentage.rb', line 46 def to_string; self.to_s + '%'; end |
#value ⇒ Object
Attributes
20 21 22 |
# File 'lib/percentage.rb', line 20 def value @value ||= 0 end |