SoftBank.Cldr.Money.to_currency

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

to_currency(money, to_currency, rates \\ ExchangeRates.latest_rates())

View Source

Specs

to_currency(
  Money.t(),
  Money.currency_code(),
  Money.ExchangeRates.t()
  | {:ok, Money.ExchangeRates.t()}
  | {:error, {module(), String.t()}}
) :: {:ok, Money.t()} | {:error, {module(), String.t()}}

Convert money from one currency to another.

Arguments

  • money is any Money.t struct returned by Cldr.Currency.new/2

  • to_currency is a valid currency code into which the money is converted

  • rates is a Map of currency rates where the map key is an upcased atom or string and the value is a Decimal conversion factor. The default is the latest available exchange rates returned from Money.ExchangeRates.latest_rates()

Examples

SoftBank.Cldr.Money.to_currency(Money.new(:USD, 100), :AUD, %{USD: Decimal.new(1), AUD: Decimal.from_float(0.7345)})
{:ok, #Money<:AUD, 73.4500>}

SoftBank.Cldr.Money.to_currency(Money.new("USD", 100), "AUD", %{"USD" => Decimal.new(1), "AUD" => Decimal.from_float(0.7345)})
{:ok, #Money<:AUD, 73.4500>}

iex> SoftBank.Cldr.Money.to_currency Money.new(:USD, 100), :AUDD, %{USD: Decimal.new(1), AUD: Decimal.from_float(0.7345)}
{:error, {Cldr.UnknownCurrencyError, "The currency :AUDD is invalid"}}

iex> SoftBank.Cldr.Money.to_currency Money.new(:USD, 100), :CHF, %{USD: Decimal.new(1), AUD: Decimal.from_float(0.7345)}
{:error, {Money.ExchangeRateError, "No exchange rate is available for currency :CHF"}}