currency_formatter v0.8.1 CurrencyFormatter

This module takes care of formatting a number to a currency. You can also request a map containing all the formatting settings for a currency.

Link to this section Summary

Functions

Returns the disambiguous symbol of a currency

Formats a number to currency

Returns a map containing the full list of currencies

Returns a List with tuples that can be used as options for select

Returns a List with tuples that can be used as options for select

Formats a number to currency wrapped in span tags with classes in a Phoenix.HTML safe way You can directly use this in your phoenix templates

Returns a map with formatting settings for a currency

Formats a number to currency wrapped in span tags with classes. This will return html as a string without any escaping. When using phoenix, consider using the format_html/1 function

Returns the symbol of a currency

Link to this section Functions

Link to this function disambiguous_symbol(currency)

Returns the disambiguous symbol of a currency

Example

iex> CurrencyFormatter.disambiguous_symbol(:AUD)
"A$"
Link to this function format(number, currency, opts \\ [])

Formats a number to currency

examples

iex> CurrencyFormatter.format(123456, :usd)
"$1,234.56"

iex> CurrencyFormatter.format(654321, :eur)
"€6.543,21"

iex> CurrencyFormatter.format(654321, "AUD")
"$6,543.21"

iex> CurrencyFormatter.format(123456, "AUD", disambiguate: true)
"A$1,234.56"
Link to this function get_currencies()
get_currencies() :: map()

Returns a map containing the full list of currencies

examples

iex> currencies = CurrencyFormatter.get_currencies()
iex> Enum.count(currencies)
172
iex> currencies["usd"]
%{"alternate_symbols" => ["US$"], "decimal_mark" => ".",
"disambiguate_symbol" => "US$", "html_entity" => "$", "iso_code" => "USD",
"iso_numeric" => "840", "name" => "United States Dollar", "priority" => 1,
"smallest_denomination" => 1, "subunit" => "Cent", "subunit_to_unit" => 100,
"symbol" => "$", "symbol_first" => true, "thousands_separator" => ","}
Link to this function get_currencies_for_select()
get_currencies_for_select() :: [{String.t(), String.t()}]

Returns a List with tuples that can be used as options for select

Examples

CurrencyFormatter.get_currencies_for_select()
["AED", "AFN", "ALL",...]
Link to this function get_currencies_for_select(format)
get_currencies_for_select(atom()) :: [{String.t(), String.t()}]

Returns a List with tuples that can be used as options for select

Examples

CurrencyFormatter.get_currencies_for_select(:names)
[{"AED", "United Arab Emirates Dirham"}, {"AFN", "Afghan Afghani"} , {"ALL", "Albanian Lek"}, ...]

CurrencyFormatter.get_currencies_for_select(:symbols)
[{"AUD", "$"}, {"CAD", "$"}, {"USD", "$"},...]

CurrencyFormatter.get_currencies_for_select(:disambiguate_symbols)
[{"AUD", "A$"}, {"CAD", "C$"}, {"USD", "$"}, ...]
Link to this function html_format(number, currency, opts \\ Keyword.new())

Formats a number to currency wrapped in span tags with classes in a Phoenix.HTML safe way You can directly use this in your phoenix templates.

example

iex> CurrencyFormatter.html_format(123456, "EUR")
[
  safe: [
    60,
    "span",
    [[32, "class", 61, 34, "currency-formatter-symbol", 34]],
    62,
    "€",
    60,
    47,
    "span",
    62
  ],
  safe: [
    60,
    "span",
    [[32, "class", 61, 34, "currency-formatter-amount", 34]],
    62,
    "1.234,56",
    60,
    47,
    "span",
    62
  ]
]
Link to this function instructions(currency \\ :USD)
instructions(atom()) :: map()
instructions(String.t()) :: map()

Returns a map with formatting settings for a currency

examples

iex> CurrencyFormatter.instructions(:EUR)
%{"alternate_symbols" => [], "decimal_mark" => ",", "html_entity" => "€",
"iso_code" => "EUR", "iso_numeric" => "978", "name" => "Euro", "priority" => 2,
"smallest_denomination" => 1, "subunit" => "Cent", "subunit_to_unit" => 100,
"symbol" => "€", "symbol_first" => true, "thousands_separator" => "."}
Link to this function raw_html_format(number, currency, opts \\ Keyword.new())

Formats a number to currency wrapped in span tags with classes. This will return html as a string without any escaping. When using phoenix, consider using the format_html/1 function

example

iex> CurrencyFormatter.raw_html_format(123456, "EUR")
~s[<span class="currency-formatter-symbol">€</span><span class="currency-formatter-amount">1.234,56</span>]

iex> CurrencyFormatter.raw_html_format(123400, "EUR", keep_decimals: true)
~s[<span class="currency-formatter-symbol">€</span><span class="currency-formatter-amount">1.234,00</span>]
Link to this function symbol(currency)
symbol(atom()) :: String.t()
symbol(atom()) :: String.t()

Returns the symbol of a currency

Example

iex> CurrencyFormatter.symbol(:AUD)
"$"