Money.sum
You're seeing just the function
sum
, go back to Money module for more information.
Specs
sum([t(), ...], Money.ExchangeRates.t()) :: {:ok, t()} | {:error, {module(), String.t()}}
Sum a list of monies that may be in different currencies.
Arguments
money_list
is a list of any validMoney.t
types returned byMoney.new/2
rates
is a map of exchange rates. The default is%{}
.Money.ExchangeRates.latest_rates/0
can be used to return the latest known exchange rates which can then applied as therates
parameter.
Returns
{:ok, money}
representing the sum of the maybe converted money amounts. The currency of the sum is the currency of the firstMoney
in themoney_list
.{:error, {exception, reason}}
describing an error.
Examples
iex> Money.sum [Money.new(:USD, 100), Money.new(:USD, 200), Money.new(:USD, 50)]
{:ok, Money.new(:USD, 350)}
iex> Money.sum [Money.new(:USD, 100), Money.new(:USD, 200), Money.new(:AUD, 50)]
{:error,
{Money.ExchangeRateError, "No exchange rate is available for currency :AUD"}}
iex> rates = %{AUD: Decimal.new(2), USD: Decimal.new(1)}
iex> Money.sum [Money.new(:USD, 100), Money.new(:USD, 200), Money.new(:AUD, 50)], rates
{:ok, Money.from_float(:USD, 325.0)}