Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dorkrawk/moola
Deal with money in Crystal
https://github.com/dorkrawk/moola
Last synced: 30 days ago
JSON representation
Deal with money in Crystal
- Host: GitHub
- URL: https://github.com/dorkrawk/moola
- Owner: dorkrawk
- Created: 2017-01-04T03:57:37.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-05-29T05:33:20.000Z (over 5 years ago)
- Last Synced: 2024-06-22T00:31:06.399Z (6 months ago)
- Language: Crystal
- Size: 42 KB
- Stars: 23
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-crystal - moola - Library for dealing with money and conversion (inspired by [RubyMoney](https://github.com/RubyMoney/money)) (Converters)
README
# Moola
A Crystal library for dealing with money (inspired by [RubyMoney](https://github.com/RubyMoney/money))## Installation
*tested with Crystal 0.28.0*
Add this to your application's `shard.yml`:
```yaml
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
```