Skip to content

arunvickram/Moneys

Folders and files

NameName
Last commit message
Last commit date

Latest commit

d4f0492 · Feb 8, 2024

History

18 Commits
Feb 8, 2024
Feb 8, 2024
Jan 26, 2024
Jan 26, 2024
Feb 8, 2024
Jan 27, 2024

Repository files navigation

Moneys

Hippocratic License HL3-BDS-CL-ECO-EXTR-FFD-LAW-MEDIA-MIL-MY-SOC-SUP-SV-TAL-USTA-XUAR Raku.land ver

Moneys is a library that's meant to be an easy-to-use, relatively safe way of handling monetary values in the Raku language. It provides a simple class Money that houses amount and currency information.

Construction

Constructing a new Money is very straightforward:

use Moneys;

my $money = Money.new(amount => 1.0, currency => 'USD'); # constructing a money value
my $money = Money.new: :amount(1.0), :currency<USD>;
my $money = Money.new(1.0, 'USD'); # arguments can also be positional

# you can also pass in a string and it'll use regexes to parse
my $money = Money.new('$1.00 USD'); 

# better yet:
with Money.new('$1.00 USD') -> $money {
    # you can really make sure your money is parsed right
} else {
    # handle errors as needed
}

If you add the :ALL pragma to use Moneys clause, you get postfix constructors for each and every currency that you can use:

use Moneys :ALL; 


my $money = 100.0USD;
my $money = 7.0AED; 

Operations

Again, relatively straightforward:

my $money-a = Money.new(5.00, 'USD');
my $money-b = Money.new(1.00, 'USD');

my $money-c = $money-a + $money-b; # => Money.new(6.00, 'USD')
my $money-d = $money-a - $money-b; # => Money.new(4.00, 'USD')

However, when doing arithmetic operations with two Moneys, you're probably going to want to do this:

my $money-a = Money.new(5.00, 'USD');
my $money-b = Money.new(1.00, 'CAD');

with $money-a + $money-b -> $money-c {
    # ... this will never happen
} else {
    # handle the error
}

Non-goals

  • Currency conversion - in order for this library to remain consistent and safe when handling conversion, we won't be handling any form of conversion.

  • Cryptocurrencies - we won't be integrating with cryptocurrencies.

About

A simple Money library for the Raku language.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages