Dinheiro v0.1.7 Moeda View Source

Build Status Coverage Status

Link to this section Summary

Types

t()

Type that represents Moeda struct with:

:name as String.t that represents the name of the currency.
:symbol as String.t that represents symbol of the currency.
:iso_code as String.t that represents the ISO 4217 code.
:country_code as integer that represents the country code.
:exponent as integer that represents the exponent of the currency

Functions

Return a map from an atom or string that represents an ISO 4217 code

Return a map from an atom or string that represents an ISO 4217 code

Return an atom from a value that represents an ISO 4217 code

Return an atom from a value that represents an ISO 4217 code

Return a multiplication factor from an ISO 4217 code

Return a multiplication factor from an ISO 4217 code

Return a formated string from a ISO 4217 code and a float value

Return a formated string from a ISO 4217 code and a float value

Link to this section Types

Link to this type t() View Source
t() :: %Moeda{
  country_code: integer(),
  exponent: integer(),
  iso_code: String.t(),
  name: String.t(),
  symbol: String.t()
}
Type that represents Moeda struct with:
:name as String.t that represents the name of the currency.
:symbol as String.t that represents symbol of the currency.
:iso_code as String.t that represents the ISO 4217 code.
:country_code as integer that represents the country code.
:exponent as integer that represents the exponent of the currency.

Link to this section Functions

Link to this function find(iso_code) View Source
find(String.t() | atom()) :: {:ok, t()} | {:error, String.t()}

Return a map from an atom or string that represents an ISO 4217 code.

Examples

iex> Moeda.find(:BRL)
{:ok, %Moeda{name: "Brazilian Real", symbol: 'R$', iso_code: "BRL", country_code: 986, exponent: 2}}
iex> Moeda.find("BRL")
{:ok, %Moeda{name: "Brazilian Real", symbol: 'R$', iso_code: "BRL", country_code: 986, exponent: 2}}
iex> Moeda.find("NONE")
{:error, "'NONE' does not represent an ISO 4217 code."}

Its function ignore case sensitive.

Examples

iex> Moeda.find(:brl)
{:ok, %Moeda{name: "Brazilian Real", symbol: 'R$', iso_code: "BRL", country_code: 986, exponent: 2}}
iex> Moeda.find("brl")
{:ok, %Moeda{name: "Brazilian Real", symbol: 'R$', iso_code: "BRL", country_code: 986, exponent: 2}}

Is possible to work with no official ISO currency code adding it in the system Mix config.

Examples

iex> Moeda.find(:XBT)
{:error, "'XBT' does not represent an ISO 4217 code."}
iex> currencies = %{ XBT: %Moeda{name: "Bitcoin", symbol: '฿', iso_code: "XBT", country_code: 0, exponent: 8} }
iex> Application.put_env(:ex_dinheiro, :unofficial_currencies, currencies)
iex> Moeda.find("xbt")
{:ok, %Moeda{name: "Bitcoin", symbol: '฿', iso_code: "XBT", country_code: 0, exponent: 8}}

Is possible to override some official ISO currency code adding it in the system Mix config.

Examples

iex> Moeda.find(:BRL)
{:ok, %Moeda{name: "Brazilian Real", symbol: 'R$', iso_code: "BRL", country_code: 986, exponent: 2}}
iex> currencies = %{ BRL: %Moeda{name: "Moeda do Brasil", symbol: 'BR$', iso_code: "BRL", country_code: 986, exponent: 4}, USD: %Moeda{name: "Moeda do EUA", symbol: 'US$', iso_code: "USD", country_code: 986, exponent: 3} }
iex> Application.put_env(:ex_dinheiro, :unofficial_currencies, currencies)
iex> Moeda.find(:BRL)
{:ok, %Moeda{name: "Moeda do Brasil", symbol: 'BR$', iso_code: "BRL", country_code: 986, exponent: 4}}
iex> Moeda.find(:USD)
{:ok, %Moeda{name: "Moeda do EUA", symbol: 'US$', iso_code: "USD", country_code: 986, exponent: 3}}
iex> Application.delete_env(:ex_dinheiro, :unofficial_currencies)
iex> Moeda.find(:BRL)
{:ok, %Moeda{name: "Brazilian Real", symbol: 'R$', iso_code: "BRL", country_code: 986, exponent: 2}}

Be careful setting new currencies to Mix config.

Examples

iex> Moeda.find(:XBT)
{:error, "'XBT' does not represent an ISO 4217 code."}
iex> currencies = %{ XBT: %{name: "Bitcoin", symbol: '฿', iso_code: "XBT", country_code: 0, exponent: 8} }
iex> Application.put_env(:ex_dinheiro, :unofficial_currencies, currencies)
iex> Moeda.find(:XBT)
{:error, ":XBT must to be associated to a Moeda struct."}
iex> currencies = %{ XBT: %Moeda{name: "Bitcoin", symbol: '฿', iso_code: "XBT", country_code: 0, exponent: 8} }
iex> Application.put_env(:ex_dinheiro, :unofficial_currencies, currencies)
iex> Moeda.find("xbt")
{:ok, %Moeda{name: "Bitcoin", symbol: '฿', iso_code: "XBT", country_code: 0, exponent: 8}}
Link to this function find!(iso_code) View Source
find!(String.t() | atom()) :: t()

Return a map from an atom or string that represents an ISO 4217 code.

Examples

iex> Moeda.find!(:BRL)
%Moeda{name: "Brazilian Real", symbol: 'R$', iso_code: "BRL", country_code: 986, exponent: 2}
iex> Moeda.find!(:NONE)
** (ArgumentError) 'NONE' does not represent an ISO 4217 code.
Link to this function get_atom(iso_code) View Source
get_atom(String.t() | atom()) :: {:ok, atom()}

Return an atom from a value that represents an ISO 4217 code.

Examples

iex> Moeda.get_atom(:BRL)
{:ok, :BRL}
iex> Moeda.get_atom(:NONE)
{:error, "'NONE' does not represent an ISO 4217 code."}
Link to this function get_atom!(iso_code) View Source
get_atom!(String.t() | atom()) :: atom()

Return an atom from a value that represents an ISO 4217 code.

Examples

iex> Moeda.get_atom!(:BRL)
:BRL
iex> Moeda.get_atom!("BRL")
:BRL
iex> Moeda.get_atom!(:NONE)
** (ArgumentError) 'NONE' does not represent an ISO 4217 code.

Its function ignore case sensitive.

Examples

iex> Moeda.get_atom!(:brl)
:BRL
iex> Moeda.get_atom!("brl")
:BRL
Link to this function get_factor(iso_code) View Source
get_factor(String.t() | atom()) :: {:ok, float()} | {:error, String.t()}

Return a multiplication factor from an ISO 4217 code.

Examples

iex> Moeda.get_factor(:BRL)
{:ok, 100.0}
iex> Moeda.get_factor(:NONE)
{:error, "'NONE' does not represent an ISO 4217 code."}
Link to this function get_factor!(iso_code) View Source
get_factor!(String.t() | atom()) :: float()

Return a multiplication factor from an ISO 4217 code.

Examples

iex> Moeda.get_factor!(:BRL)
100.0
iex> Moeda.get_factor!("BRL")
100.0
iex> Moeda.get_factor!(:NONE)
** (ArgumentError) 'NONE' does not represent an ISO 4217 code.

Its function ignore case sensitive.

Examples

iex> Moeda.get_factor!(:brl)
100.0
iex> Moeda.get_factor!("brl")
100.0
Link to this function to_string(currency, valor, opts \\ []) View Source
to_string(String.t() | atom(), float(), Keywords.t()) ::
  {:ok, String.t()} | {:error, String.t()}

Return a formated string from a ISO 4217 code and a float value.

Examples

iex> Moeda.to_string(:BRL, 100.0)
{:ok, "R$ 100,00"}
iex> Moeda.to_string(:NONE, 1000.5)
{:error, "'NONE' does not represent an ISO 4217 code."}
Link to this function to_string!(currency, valor, opts \\ []) View Source
to_string!(String.t() | atom(), float(), Keywords.t()) :: String.t()

Return a formated string from a ISO 4217 code and a float value.

Examples

iex> Moeda.to_string!(:BRL, 100.0)
"R$ 100,00"
iex> Moeda.to_string!("BRL", 1000.5)
"R$ 1.000,50"
iex> Moeda.to_string!(:BRL, -1.0)
"R$ -1,00"
iex> Moeda.to_string!(:NONE, 1000.5)
** (ArgumentError) 'NONE' does not represent an ISO 4217 code.

Its function ignore case sensitive.

Examples

iex> Moeda.to_string!(:bRl, 100.0)
"R$ 100,00"
iex> Moeda.to_string!("BrL", 1000.5)
"R$ 1.000,50"

Using options-style parameters you can change the behavior of the function.

  • thousand_separator - default ".", set the thousand separator.
  • decimal_separator - default ",", set the decimal separator.
  • display_currency_symbol - default true, put to false to hide de currency symbol.
  • display_currency_code - default false, put to true to display de currency ISO 4217 code.

Exemples

iex> Moeda.to_string!(:USD, 1000.5, thousand_separator: ",", decimal_separator: ".")
"$ 1,000.50"
iex> Moeda.to_string!(:USD, 1000.5, display_currency_symbol: false)
"1.000,50"
iex> Moeda.to_string!(:USD, 1000.5, display_currency_code: true)
"$ 1.000,50 USD"
iex> Moeda.to_string!(:USD, 1000.5, display_currency_code: true, display_currency_symbol: false)
"1.000,50 USD"

The default values also can be set in the system Mix config.

Example:

iex> Application.put_env(:ex_dinheiro, :thousand_separator, ",")
iex> Application.put_env(:ex_dinheiro, :decimal_separator, ".")
iex> Moeda.to_string!(:USD, 1000.5)
"$ 1,000.50"
iex> Application.put_env(:ex_dinheiro, :display_currency_symbol, false)
iex> Moeda.to_string!(:USD, 5000.5)
"5,000.50"
iex> Application.put_env(:ex_dinheiro, :display_currency_code, true)
iex> Moeda.to_string!(:USD, 10000.0)
"10,000.00 USD"

The options-style parameters override values in the system Mix config.

Example:

iex> Application.put_env(:ex_dinheiro, :thousand_separator, ",")
iex> Application.put_env(:ex_dinheiro, :decimal_separator, ".")
iex> Moeda.to_string!(:USD, 1000.5)
"$ 1,000.50"
iex> Moeda.to_string!(:BRL, 1000.5, thousand_separator: ".", decimal_separator: ",")
"R$ 1.000,50"