Class: Docdata::Order::Amount

Inherits:
Object
  • Object
show all
Defined in:
lib/docdata/order/amount.rb

Overview

Helper for converting/formatting amounts.

Class Method Summary collapse

Instance Method Summary collapse

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_amountObject



23
24
25
# File 'lib/docdata/order/amount.rb', line 23

def to_amount
  @amount / 100.0
end

#to_centsObject



27
28
29
30
# File 'lib/docdata/order/amount.rb', line 27

def to_cents
  cents = @amount * 100
  cents.to_i
end

#to_dObject



19
20
21
# File 'lib/docdata/order/amount.rb', line 19

def to_d
  @amount
end

#to_sObject

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