Cldr Currencies v2.1.4 Cldr.Currency View Source
Defines a currency structure and a set of functions to manage the validity of a currency code and to return metadata for currencies.
Link to this section Summary
Functions
Returns a map of the metadata for all currencies for a given locale
Returns a map of the metadata for all currencies for a given locale and raises on error
Return only those currencies meeting the filter criteria
Returns the currency metadata for the requested currency code
Returns a map that matches a currency string to a currency code
Returns a map that matches a currency string to a currency code or raises an exception
Returns a list of all known currency codes
Returns a boolean indicating if the supplied currency code is known
Returns a valid normalized ISO4217 format custom currency code or an error
Returns a Currency
struct created from the arguments
Returns the appropriate currency display name for the currency
, based
on the plural rules in effect for the locale
Returns the strings associated with a currency in a given locale
Link to this section Types
code()
View Source
code() :: String.t()
code() :: String.t()
currency_status()
View Source
currency_status() :: :all | :current | :historic | :tender | :unannotated
currency_status() :: :all | :current | :historic | :tender | :unannotated
format()
View Source
format() :: :standard | :accounting | :short | :long | :percent | :scientific
format() :: :standard | :accounting | :short | :long | :percent | :scientific
t()
View Source
t() :: %Cldr.Currency{
cash_digits: non_neg_integer(),
cash_rounding: non_neg_integer(),
code: code(),
count: %{},
digits: non_neg_integer(),
from: Date.year(),
iso_digits: non_neg_integer(),
name: String.t(),
narrow_symbol: String.t(),
rounding: non_neg_integer(),
symbol: String.t(),
tender: boolean(),
to: Date.year()
}
t() :: %Cldr.Currency{ cash_digits: non_neg_integer(), cash_rounding: non_neg_integer(), code: code(), count: %{}, digits: non_neg_integer(), from: Date.year(), iso_digits: non_neg_integer(), name: String.t(), narrow_symbol: String.t(), rounding: non_neg_integer(), symbol: String.t(), tender: boolean(), to: Date.year() }
Link to this section Functions
currencies_for_locale(locale, backend, currency_status \\ :all)
View Source
currencies_for_locale(
Cldr.Locale.name() | Cldr.LanguageTag.t(),
Cldr.backend(),
currency_status()
) :: {:ok, Map.t()} | {:error, {Exception.t(), String.t()}}
currencies_for_locale( Cldr.Locale.name() | Cldr.LanguageTag.t(), Cldr.backend(), currency_status() ) :: {:ok, Map.t()} | {:error, {Exception.t(), String.t()}}
Returns a map of the metadata for all currencies for a given locale.
Arguments
locale
is any valid locale name returned byCldr.known_locale_names/1
or aCldr.LanguageTag
struct returned byCldr.Locale.new!/2
backend
is any module that includesuse Cldr
and therefore is aCldr
backend modulecurrency_status
is:all
,:current
,:historic
,unannotated
or:tender
; or a list of one or more status. The default is:all
. SeeCldr.Currency.currency_filter/2
.
Returns
{:ok, currency_map}
or{:error, {exception, reason}}
Example
=> Cldr.Currency.currencies_for_locale "en", MyApp.Cldr {:ok, %{
FJD: %Cldr.Currency{
cash_digits: 2,
cash_rounding: 0,
code: "FJD",
count: %{one: "Fijian dollar", other: "Fijian dollars"},
digits: 2,
from: nil,
iso_digits: 2,
name: "Fijian Dollar",
narrow_symbol: "$",
rounding: 0,
symbol: "FJD",
tender: true,
to: nil
},
SUR: %Cldr.Currency{
cash_digits: 2,
cash_rounding: 0,
code: "SUR",
count: %{one: "Soviet rouble", other: "Soviet roubles"},
digits: 2,
from: nil,
iso_digits: nil,
name: "Soviet Rouble",
narrow_symbol: nil,
rounding: 0,
symbol: "SUR",
tender: true,
to: nil
},
...
}}
currencies_for_locale!(locale, backend, currency_status \\ :all)
View Source
currencies_for_locale!(
Cldr.Locale.name() | Cldr.LanguageTag.t(),
Cldr.backend(),
currency_status()
) :: Map.t() | no_return()
currencies_for_locale!( Cldr.Locale.name() | Cldr.LanguageTag.t(), Cldr.backend(), currency_status() ) :: Map.t() | no_return()
Returns a map of the metadata for all currencies for a given locale and raises on error.
Arguments
locale
is any valid locale name returned byCldr.known_locale_names/1
or aCldr.LanguageTag
struct returned byCldr.Locale.new!/2
backend
is any module that includesuse Cldr
and therefore is aCldr
backend modulecurrency_status
is:all
,:current
,:historic
,unannotated
or:tender
; or a list of one or more status. The default is:all
. SeeCldr.Currency.currency_filter/2
.
Returns
{:ok, currency_map}
orraises an exception
Example
=> MyApp.Cldr.Currency.currencies_for_locale! "en" %{
FJD: %Cldr.Currency{
cash_digits: 2,
cash_rounding: 0,
code: "FJD",
count: %{one: "Fijian dollar", other: "Fijian dollars"},
digits: 2,
from: nil,
iso_digits: 2,
name: "Fijian Dollar",
narrow_symbol: "$",
rounding: 0,
symbol: "FJD",
tender: true,
to: nil
},
SUR: %Cldr.Currency{
cash_digits: 2,
cash_rounding: 0,
code: "SUR",
count: %{one: "Soviet rouble", other: "Soviet roubles"},
digits: 2,
from: nil,
iso_digits: nil,
name: "Soviet Rouble",
narrow_symbol: nil,
rounding: 0,
symbol: "SUR",
tender: true,
to: nil
},
...
}
currency_filter(currency, currency_status)
View Source
currency_filter(
Cldr.Currency.t() | [Cldr.Currency.t()] | Map.t(),
Cldr.Currency.currency_status()
) :: boolean()
currency_filter( Cldr.Currency.t() | [Cldr.Currency.t()] | Map.t(), Cldr.Currency.currency_status() ) :: boolean()
Return only those currencies meeting the filter criteria.
Arguments
currency
is aCldr.Currency.t
, a list ofCldr.Currency.t
or a map where the values of each item is aCldr.Currency.t
currency_status
is:all
,:current
,:historic
,:tender
unannotated
or a list of one or more status. The default is:all
Currency Status
A currency may be in current use, of historic interest only. It may or may not be legal tender. And it may mostly be used as a financial instrument. To help return the most useful currencies the currency status code acts as follows:
:all
, the default, returns all currencies:current
returns those currencies that have a:to
date of nil and which also is a known ISO4217 currency:historic
is the opposite of:current
:tender
is a currency that is legal tender:unannotated
is a currency that doesn't have "(some string)" in its name. These are usually financial instruments.
currency_for_code(currency_code, backend, options \\ [])
View Source
currency_for_code(code(), Cldr.backend(), Keyword.t()) ::
{:ok, t()} | {:error, {Exception.t(), String.t()}}
currency_for_code(code(), Cldr.backend(), Keyword.t()) :: {:ok, t()} | {:error, {Exception.t(), String.t()}}
Returns the currency metadata for the requested currency code.
Arguments
currency_code
is abinary
oratom
representation of an ISO 4217 currency code.backend
is any module that includesuse Cldr
and therefore is aCldr
backend moduleoptions
is aKeyword
list of options.
Options
:locale
is any valid locale name returned byCldr.known_locale_names/1
or aCldr.LanguageTag
struct returned byCldr.Locale.new!/2
Examples
iex> Cldr.Currency.currency_for_code("AUD", Test.Cldr)
{:ok,
%Cldr.Currency{
cash_digits: 2,
cash_rounding: 0,
code: "AUD",
count: %{one: "Australian dollar", other: "Australian dollars"},
digits: 2,
iso_digits: 2,
name: "Australian Dollar",
narrow_symbol: "$",
rounding: 0,
symbol: "A$",
tender: true
}}
iex> Cldr.Currency.currency_for_code("THB", Test.Cldr)
{:ok,
%Cldr.Currency{
cash_digits: 2,
cash_rounding: 0,
code: "THB",
count: %{one: "Thai baht", other: "Thai baht"},
digits: 2,
iso_digits: 2,
name: "Thai Baht",
narrow_symbol: "฿",
rounding: 0,
symbol: "THB",
tender: true
}}
currency_strings(locale, backend, currency_status \\ :all)
View Source
currency_strings(Cldr.Locale.t(), Cldr.backend(), currency_status()) ::
{:ok, Map.t()} | {:error, {Exception.t(), String.t()}}
currency_strings(Cldr.Locale.t(), Cldr.backend(), currency_status()) :: {:ok, Map.t()} | {:error, {Exception.t(), String.t()}}
Returns a map that matches a currency string to a currency code.
A currency string is a localised name or symbol representing a currency in a locale-specific manner.
Arguments
locale
is any valid locale name returned byCldr.known_locale_names/1
or aCldr.LanguageTag
struct returned byCldr.Locale.new!/2
.currency_status
is:all
,:current
,:historic
,unannotated
or:tender
; or a list of one or more status. The default is:all
. SeeCldr.Currency.currency_filter/2
.
Returns
{:ok, currency_string_map}
or{:error, {exception, reason}}
Example
=> Cldr.Currency.currency_strings "en", MyApp.Cldr
{:ok,
%{
"mexican silver pesos" => :MXP,
"sudanese dinar" => :SDD,
"bad" => :BAD,
"rsd" => :RSD,
"swazi lilangeni" => :SZL,
"zairean new zaire" => :ZRN,
"guyanaese dollars" => :GYD,
"equatorial guinean ekwele" => :GQE,
...
}}
# Currencies match all currency status'
=> Cldr.Currency.currency_strings "en", MyApp.Cldr, [:tender, :current, :unannotated]
{:ok,
%{
"rsd" => :RSD,
"swazi lilangeni" => :SZL,
"guyanaese dollars" => :GYD,
"syrian pound" => :SYP,
"scr" => :SCR,
"bangladeshi takas" => :BDT,
"netherlands antillean guilders" => :ANG,
"pen" => :PEN,
...
}}
currency_strings!(locale, backend, currency_status \\ :all)
View Source
currency_strings!(Cldr.Locale.t(), Cldr.backend(), currency_status()) ::
Map.t() | no_return()
currency_strings!(Cldr.Locale.t(), Cldr.backend(), currency_status()) :: Map.t() | no_return()
Returns a map that matches a currency string to a currency code or raises an exception.
A currency string is a localised name or symbol representing a currency in a locale-specific manner.
Arguments
locale
is any valid locale name returned byCldr.known_locale_names/1
or aCldr.LanguageTag
struct returned byCldr.Locale.new!/2
.currency_status
is:all
,:current
,:historic
,unannotated
or:tender
; or a list of one or more status. The default is:all
. SeeCldr.Currency.currency_filter/2
.
Returns
{:ok, currency_string_map}
orraises an exception
Example
=> Cldr.Currency.currency_strings! "en", MyApp.Cldr
%{
"mexican silver pesos" => :MXP,
"sudanese dinar" => :SDD,
"bad" => :BAD,
"rsd" => :RSD,
"swazi lilangeni" => :SZL,
"zairean new zaire" => :ZRN,
"guyanaese dollars" => :GYD,
"equatorial guinean ekwele" => :GQE,
...
}
current?(currency) View Source
historic?(currency) View Source
known_currencies()
View Source
known_currencies() :: [atom()]
known_currencies() :: [atom()]
Returns a list of all known currency codes.
Example
iex> Cldr.Currency.known_currencies |> Enum.count
303
known_currency?(currency_code, custom_currencies \\ []) View Source
Returns a boolean indicating if the supplied currency code is known.
Arguments
currency_code
is abinary
oratom
representing an ISO4217 currency codecustom_currencies
is an optional list of custom currencies created by theCldr.Currency.new/2
function
Returns
true
orfalse
Examples
iex> Cldr.Currency.known_currency? "AUD"
true
iex> Cldr.Currency.known_currency? "GGG"
false
iex> Cldr.Currency.known_currency? :XCV
false
iex> Cldr.Currency.known_currency? :XCV, [%Cldr.Currency{code: :XCV}]
true
make_currency_code(code) View Source
Returns a valid normalized ISO4217 format custom currency code or an error.
Currency codes conform to the ISO4217 standard which means that any custom currency code must start with an "X" followed by two alphabetic characters.
Note that since this function creates atoms but to a maximum of 26 * 26 == 676 since the format permits 2 alphabetic characters only.
Arguments
currency_code
is aString.t
or andatom
representing the new currency code to be created
Returns
{:ok, currency_code}
or{:error, {exception, message}}
Examples
iex> Cldr.Currency.make_currency_code("xzz")
{:ok, :XZZ}
iex> Cldr.Currency.make_currency_code("aaa")
{:error, {Cldr.CurrencyCodeInvalid,
"Invalid currency code \"AAA\". Currency codes must start with 'X' followed by 2 alphabetic characters only."}}
new(currency, options \\ []) View Source
Returns a Currency
struct created from the arguments.
Arguments
currency
is a custom currency code of a format defined in ISO4217options
is a map of options representing the optional elements of the%Currency{}
struct
Returns
{:ok, Cldr.Currency.t}
or{:error, {exception, message}}
Example
iex> Cldr.Currency.new(:XAA)
{:ok,
%Cldr.Currency{cash_digits: 0, cash_rounding: 0, code: :XAA, count: nil,
digits: 0, name: "", narrow_symbol: nil, rounding: 0, symbol: "",
tender: false}}
iex> Cldr.Currency.new(:ZAA, name: "Invalid Custom Name")
{:error, {Cldr.UnknownCurrencyError, "The currency :ZAA is invalid"}}
iex> Cldr.Currency.new("xaa", name: "Custom Name")
{:ok,
%Cldr.Currency{cash_digits: 0, cash_rounding: 0, code: :XAA, count: nil,
digits: 0, name: "Custom Name", narrow_symbol: nil, rounding: 0, symbol: "",
tender: false}}
iex> Cldr.Currency.new(:XAA, name: "Custom Name")
{:ok,
%Cldr.Currency{cash_digits: 0, cash_rounding: 0, code: :XAA, count: nil,
digits: 0, name: "Custom Name", narrow_symbol: nil, rounding: 0, symbol: "",
tender: false}}
iex> Cldr.Currency.new(:XBC)
{:error, {Cldr.CurrencyAlreadyDefined, "Currency :XBC is already defined"}}
pluralize(number, currency, backend, options \\ []) View Source
Returns the appropriate currency display name for the currency
, based
on the plural rules in effect for the locale
.
Arguments
number
is an integer, float orDecimal
currency
is any currency returned byCldr.Currency.known_currencies/0
locale
is any valid locale name returned byCldr.known_locale_names/1
or aCldr.LanguageTag
struct returned byCldr.Locale.new!/2
backend
is any module that includesuse Cldr
and therefore is aCldr
backend moduleoptions
is a keyword list of options
Options
:locale
is any locale returned byCldr.Locale.new!/2
. The default isCldr.get_current_locale/1
Returns
{:ok, plural_string}
or{:error, {exception, message}}
Examples
iex> Cldr.Currency.pluralize 1, :USD, Test.Cldr
{:ok, "US dollar"}
iex> Cldr.Currency.pluralize 3, :USD, Test.Cldr
{:ok, "US dollars"}
iex> Cldr.Currency.pluralize 12, :USD, Test.Cldr, locale: "zh"
{:ok, "美元"}
iex> Cldr.Currency.pluralize 12, :USD, Test.Cldr, locale: "fr"
{:ok, "dollars des États-Unis"}
iex> Cldr.Currency.pluralize 1, :USD, Test.Cldr, locale: "fr"
{:ok, "dollar des États-Unis"}
strings_for_currency(currency, locale, backend) View Source
Returns the strings associated with a currency in a given locale.
Arguments
currency
is an ISO4217 currency codelocale
is any valid locale name returned byCldr.known_locale_names/1
or aCldr.LanguageTag
struct returned byCldr.Locale.new!/2
.backend
is any module that includesuse Cldr
and therefore is aCldr
backend module
Returns
A list of strings or
{:error, {exception, reason}}
Example
iex> Cldr.Currency.strings_for_currency :AUD, "en", Test.Cldr
["a$", "australian dollars", "aud", "australian dollar"]
iex> Cldr.Currency.strings_for_currency :AUD, "de", Test.Cldr
["australische dollar", "australischer dollar", "au$", "aud"]
iex> Cldr.Currency.strings_for_currency :AUD, "zh", Test.Cldr
["澳大利亚元", "au$", "aud"]