Class: Decimal
- Inherits:
-
Object
- Object
- Decimal
- Defined in:
- lib/ebutil.rb
Class Method Summary collapse
- .from_string(string, thousand_sep = '.', decimal_sep = ',') ⇒ Object
- .from_whole_and_part(whole, part = 0) ⇒ Object
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #==(other) ⇒ Object
-
#initialize(value) ⇒ Decimal
constructor
A new instance of Decimal.
- #inverse ⇒ Object
- #part ⇒ Object
- #sign ⇒ Object
- #to_s(decimal_sep = ',') ⇒ Object (also: #inspect)
- #whole ⇒ Object
Constructor Details
#initialize(value) ⇒ Decimal
Returns a new instance of Decimal.
76 77 78 |
# File 'lib/ebutil.rb', line 76 def initialize(value) @value = value end |
Class Method Details
.from_string(string, thousand_sep = '.', decimal_sep = ',') ⇒ Object
80 81 82 83 |
# File 'lib/ebutil.rb', line 80 def self.from_string(string, thousand_sep = '.', decimal_sep = ',') string = string.to_s from_whole_and_part(*string.split(decimal_sep)) end |
.from_whole_and_part(whole, part = 0) ⇒ Object
85 86 87 |
# File 'lib/ebutil.rb', line 85 def self.from_whole_and_part(whole, part = 0) new( whole.to_i * 100 + part.to_i * (whole.to_i >= 0 ? 1 : -1)) end |
Instance Method Details
#+(other) ⇒ Object
101 102 103 |
# File 'lib/ebutil.rb', line 101 def +(other) return Decimal.from_value(value+other.value) end |
#-(other) ⇒ Object
105 106 107 |
# File 'lib/ebutil.rb', line 105 def -(other) return Decimal.from_value(value - other.value) end |
#==(other) ⇒ Object
118 119 120 |
# File 'lib/ebutil.rb', line 118 def ==(other) value == other.value end |
#inverse ⇒ Object
109 110 111 |
# File 'lib/ebutil.rb', line 109 def inverse return Decimal.new(-value) end |
#part ⇒ Object
97 98 99 |
# File 'lib/ebutil.rb', line 97 def part value.abs % 100 end |
#sign ⇒ Object
89 90 91 |
# File 'lib/ebutil.rb', line 89 def sign value >= 0 ? 1 : -1 end |
#to_s(decimal_sep = ',') ⇒ Object Also known as: inspect
113 114 115 |
# File 'lib/ebutil.rb', line 113 def to_s(decimal_sep = ',') sprintf("#{string_sign}%d#{decimal_sep}%02d", whole, part) end |
#whole ⇒ Object
93 94 95 |
# File 'lib/ebutil.rb', line 93 def whole value.abs / 100 end |