mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-04 21:15:19 +02:00
Add Money and Money Series (#505)
* Add Money class * Standardize creation of money series * Formatting * Fix test
This commit is contained in:
parent
89ea12e9a1
commit
0fe9b6d34a
16 changed files with 228 additions and 161 deletions
32
app/models/money.rb
Normal file
32
app/models/money.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
class Money
|
||||
attr_reader :amount, :currency
|
||||
|
||||
def self.from_amount(amount, currency = "USD")
|
||||
Money.new(amount, currency)
|
||||
end
|
||||
|
||||
def initialize(amount, currency = :USD)
|
||||
@amount = amount
|
||||
@currency = currency
|
||||
end
|
||||
|
||||
def cents(precision: nil)
|
||||
_precision = precision || CURRENCY_OPTIONS[@currency.to_sym][:precision]
|
||||
return "" unless _precision.positive?
|
||||
|
||||
fractional_part = @amount.to_s.split(".")[1] || ""
|
||||
fractional_part = fractional_part[0, _precision].ljust(_precision, "0")
|
||||
end
|
||||
|
||||
def symbol
|
||||
CURRENCY_OPTIONS[@currency.to_sym][:symbol]
|
||||
end
|
||||
|
||||
def separator
|
||||
CURRENCY_OPTIONS[@currency.to_sym][:separator]
|
||||
end
|
||||
|
||||
def precision
|
||||
CURRENCY_OPTIONS[@currency.to_sym][:precision]
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue