Class: Tikkie::Api::Amount
- Inherits:
-
Object
- Object
- Tikkie::Api::Amount
- Defined in:
- lib/tikkie/api/amount.rb
Overview
Helper for converting amounts to cents and back.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(amount) ⇒ Amount
constructor
A new instance of Amount.
- #to_cents ⇒ Object
- #to_d ⇒ Object
-
#to_s ⇒ Object
Convert the amount to a String with 2 decimals.
Constructor Details
#initialize(amount) ⇒ Amount
Returns a new instance of Amount.
16 17 18 |
# File 'lib/tikkie/api/amount.rb', line 16 def initialize(amount) @amount = BigDecimal(amount.to_s) end |
Class Method Details
.from_cents(cents) ⇒ Object
10 11 12 13 |
# File 'lib/tikkie/api/amount.rb', line 10 def from_cents(cents) amount = BigDecimal(cents) / 100 new(amount) end |
Instance Method Details
#to_cents ⇒ Object
29 30 31 32 |
# File 'lib/tikkie/api/amount.rb', line 29 def to_cents cents = @amount * 100 cents.to_i end |
#to_d ⇒ Object
20 21 22 |
# File 'lib/tikkie/api/amount.rb', line 20 def to_d @amount end |
#to_s ⇒ Object
Convert the amount to a String with 2 decimals.
25 26 27 |
# File 'lib/tikkie/api/amount.rb', line 25 def to_s format("%.2f", @amount) end |