Monet

Handle currency conversion and common operations (addition, subtraction, multiplication, division).

How it works

It defines some base types:

Dangers

Even this library isn't safe from precision losses. For example, an Exponent's amount could be cut out by its exponent. Also errors when converting money could occurr.

Examples

Summing two Moneys of the same type

```rust

use monet::{Money, CurrencyAmount, Rates, Operation}; use std::convert::TryInto;

// Custom rates. let map = vec![("USD", 1000000)].intoiter() .map(|(code, worth)| (code.tryinto().unwrap(), worth.into()) .collect(); let rates = Rates::with_rates(map);

let moneyowned = Money::withstrcode(CurrencyAmount::withunit(2), "USD").unwrap(); let moneypaid = Money::withstrcode(CurrencyAmount::withunit(1), "USD").unwrap();

let remaining = (moneyowned - moneypaid).execute(&rates);

asserteq!(remaining, Money::withstrcode(CurrencyAmount::withunit(1), "USD")); asserteq!(remaining, Money::withstrcode(1000_000.into(), "USD"));

```

Summing two Moneys of different type

```rust

use monet::{Money, CurrencyAmount, Rates, Operation}; use std::convert::TryInto;

// Custom rates. let map = vec![ ("USD", 1000000), ("CHF", 1100000), ].intoiter() .map(|(code, worth)| (code.tryinto().unwrap(), worth.into()) .collect();

let rates = Rates::with_rates(map);

let moneyone = Money::withstrcode(1000000.into(), "CHF").unwrap(); let moneytwo = Money::withstrcode(1100000.into(), "USD").unwrap();

// Note: sum has currency code "CHF": when summing, // the currency code of the first money is used let sum = (moneyone + moneytwo).execute(&rates);

asserteq!(remaining, Money::withstrcode(2000_000.into(), "CHF"));

```

Chaining operations

```rust

use monet::{Money, CurrencyAmount, Rates, Operation}; use std::convert::TryInto;

// Custom rates. let map = vec![ ("USD", 1000000), ("CHF", 1100000), ].intoiter() .map(|(code, worth)| (code.tryinto().unwrap(), worth.into()) .collect();

let rates = Rates::with_rates(map);

let moneyone = Money::withstrcode(1000000.into(), "CHF").unwrap(); let moneytwo = Money::withstrcode(1100000.into(), "USD").unwrap(); let moneythree = Money::withstrcode(2000_000.into(), "CHF").unwrap();

// Note: sum has currency code "CHF" let sum = (moneyone + moneytwo - money_three).execute(&rates);

asserteq!(remaining, Money::withstr_code(0.into(), "CHF"));

```