Dinheiro v0.2.1 Moeda View Source
Link to this section Summary
Types
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.
:alpha_code as String.t that represents the alphabetic ISO 4217 code.
:num_code as integer that represents the numeric ISO 4217 code. Where possible the 3 digit numeric code is the same as the numeric 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
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.
:alpha_code as String.t that represents the alphabetic ISO 4217 code.
:num_code as integer that represents the numeric ISO 4217 code. Where possible the 3 digit numeric code is the same as the numeric country code.
:exponent as integer that represents the exponent of the currency.
Link to this section Functions
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$', alpha_code: "BRL", num_code: 986, exponent: 2}}
iex> Moeda.find("BRL")
{:ok, %Moeda{name: "Brazilian Real", symbol: 'R$', alpha_code: "BRL", num_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$', alpha_code: "BRL", num_code: 986, exponent: 2}}
iex> Moeda.find("brl")
{:ok, %Moeda{name: "Brazilian Real", symbol: 'R$', alpha_code: "BRL", num_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: '฿', alpha_code: "XBT", num_code: 0, exponent: 8} }
iex> Application.put_env(:ex_dinheiro, :unofficial_currencies, currencies)
iex> Moeda.find("xbt")
{:ok, %Moeda{name: "Bitcoin", symbol: '฿', alpha_code: "XBT", num_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$', alpha_code: "BRL", num_code: 986, exponent: 2}}
iex> currencies = %{ BRL: %Moeda{name: "Moeda do Brasil", symbol: 'BR$', alpha_code: "BRL", num_code: 986, exponent: 4}, USD: %Moeda{name: "Moeda do EUA", symbol: 'US$', alpha_code: "USD", num_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$', alpha_code: "BRL", num_code: 986, exponent: 4}}
iex> Moeda.find(:USD)
{:ok, %Moeda{name: "Moeda do EUA", symbol: 'US$', alpha_code: "USD", num_code: 986, exponent: 3}}
iex> Application.delete_env(:ex_dinheiro, :unofficial_currencies)
iex> Moeda.find(:BRL)
{:ok, %Moeda{name: "Brazilian Real", symbol: 'R$', alpha_code: "BRL", num_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: '฿', alpha_code: "XBT", num_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: '฿', alpha_code: "XBT", num_code: 0, exponent: 8} }
iex> Application.put_env(:ex_dinheiro, :unofficial_currencies, currencies)
iex> Moeda.find("xbt")
{:ok, %Moeda{name: "Bitcoin", symbol: '฿', alpha_code: "XBT", num_code: 0, exponent: 8}}
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$', alpha_code: "BRL", num_code: 986, exponent: 2}
iex> Moeda.find!(:NONE)
** (ArgumentError) 'NONE' does not represent an ISO 4217 code
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"}
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
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"}
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
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"}
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
- defaulttrue
, put tofalse
to hide de currency symbol.display_currency_code
- defaultfalse
, put totrue
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"