Money.sum

You're seeing just the function sum, go back to Money module for more information.
Link to this function

sum(money_list, rates \\ %{})

View Source (since 5.3.0)

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 valid Money.t types returned by Money.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 the rates parameter.

Returns

  • {:ok, money} representing the sum of the maybe converted money amounts. The currency of the sum is the currency of the first Money in the money_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)}