Monetized v0.5.0 Monetized.Math

This module defines arithmetical operations on money.

All functions in this module take either money structs as parameters from which the currency for the result is inferred or if you don’t care for the currency, any of the Monetized.Money.make/2 supported values.

A error will be raised if the money structs hold distinct non nil values.

Summary

Functions

Adds two values and returns a money struct with the result

Substracts money from money returning a money struct with the result

Functions

add(a, b)

Specs

add(Monetized.Money.t | String.t | integer | float | Decimal, Monetized.Money.t | String.t | integer | float | Decimal) :: Monetized.Money.t

Adds two values and returns a money struct with the result.

Examples

iex> value_one = Monetized.Money.make(10)
...> value_two = Monetized.Money.make(20.50)
...> Monetized.Math.add(value_one, value_two)
#Money<30.50>

iex> five_euros = Monetized.Money.make("€ 5")
...> result = Monetized.Math.add(five_euros, 20)
...> Monetized.Money.to_string(result, [currency_symbol: true])
"€ 25.00"

iex> Monetized.Math.add("£ 100", "£ 1,350.25")
#Money<1450.25GBP>
sub(a, b)

Specs

sub(Monetized.Money.t | String.t | integer | float | Decimal, Monetized.Money.t | String.t | integer | float | Decimal) :: Monetized.Money.t

Substracts money from money returning a money struct with the result.

Examples

iex> payment_one = Monetized.Money.make(50)
...> payment_two = Monetized.Money.make(51, [currency: "EUR"])
...> Monetized.Math.sub(payment_one, payment_two)
#Money<-1.00EUR>

iex> payment_one = Monetized.Money.make(2000)
...> payment_two = Monetized.Money.make(150.25)
...> result = Monetized.Math.sub(payment_one, payment_two)
...> Monetized.Money.to_string(result)
"1,849.75"

iex> result = Monetized.Math.sub(100.50, 200)
...> Monetized.Money.to_string(result)
"-99.50"

iex> result = Monetized.Math.sub("£ -100", "1,200.00")
...> Monetized.Money.to_string(result, [currency_symbol: true])
"£ -1,300.00"