Class: Tikkie::Api::Amount

Inherits:
Object
  • Object
show all
Defined in:
lib/tikkie/api/amount.rb

Overview

Helper for converting amounts to cents and back.

Class Method Summary collapse

Instance Method Summary collapse

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_centsObject



29
30
31
32
# File 'lib/tikkie/api/amount.rb', line 29

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

#to_dObject



20
21
22
# File 'lib/tikkie/api/amount.rb', line 20

def to_d
  @amount
end

#to_sObject

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