moola

Library for dealing with money and conversion (inspired by RubyMoney)
HEAD Latest release released
dorkrawk/moola
23 1
Dave Schwantes

Moola

A Crystal library for dealing with money (inspired by RubyMoney)

Installation

tested with Crystal 0.28.0

Add this to your application's shard.yml:

dependencies:
  moola:
    github: dorkrawk/moola

Usage

Basic Money object

money = Moola.new(42_00) # creates a new Moola::Money object
money.format # "$42.00"
money.cents  # 4200
money.zero?  # false
money.to_f # 42.0
-money == Moola.new(-42_00) # true 

Comparisons

Moola::Money.new(5_00) == Moola::Money.new(5_00) # true
Moola::Money.new(5_00) == Moola::Money.new(7_00) # false
Moola::Money.new(5_00, "USD") == Moola::Money.new(5_00, "EUR") # false
[Moola::Money.new(5_00), Moola::Money.new(2_00), Moola::Money.new(7_00)].sort # sorted by amount

Arithmetic

Moola::Money.new(5_00) + Moola::Money.new(10_00) == Moola::Money.new(15_00)
Moola::Money.new(15_00) + Moola::Money.new(12_00) == Moola::Money.new(3_00)
Moola::Money.new(15_00) / 3 == Moola::Money.new(5_00)
Moola::Money.new(5_00) * 3 == Moola::Money.new(15_00)

Conversion

Moola::Exchange.add_conversion(Moola::Currency.find("usd"), Moola::Currency.find("cad"), 1.3)
money = Moola.new(1_00, "USD")
money.convert_to(Moola::Currency.find("cad")) == Moola.new(1_30, "cad")

Testing

# from the project root...
crystal spec
moola:
  github: dorkrawk/moola
  
Crystal none

Authors

Dependencies 0

Development Dependencies 0

Dependents 0

Last synced .
search fire star recently