Class: Docdata::Order::Amount
- Inherits:
-
Object
- Object
- Docdata::Order::Amount
- Defined in:
- lib/docdata/order/amount.rb
Overview
Helper for converting/formatting amounts.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(amount) ⇒ Amount
constructor
A new instance of Amount.
- #to_amount ⇒ Object
- #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.
15 16 17 |
# File 'lib/docdata/order/amount.rb', line 15 def initialize(amount) @amount = BigDecimal(amount.to_s) end |
Class Method Details
.from_cents(cents) ⇒ Object
10 11 12 |
# File 'lib/docdata/order/amount.rb', line 10 def from_cents(cents) new(cents.to_i / 100.0) end |
Instance Method Details
#to_amount ⇒ Object
23 24 25 |
# File 'lib/docdata/order/amount.rb', line 23 def to_amount @amount / 100.0 end |
#to_cents ⇒ Object
27 28 29 30 |
# File 'lib/docdata/order/amount.rb', line 27 def to_cents cents = @amount * 100 cents.to_i end |
#to_d ⇒ Object
19 20 21 |
# File 'lib/docdata/order/amount.rb', line 19 def to_d @amount end |
#to_s ⇒ Object
Convert the amount to a String with 2 decimals.
33 34 35 |
# File 'lib/docdata/order/amount.rb', line 33 def to_s format("%.2f", @amount) end |