Functions for parsing numbers and currencies from strings in a locale-aware manner.
The parser handles locale-specific digit transliteration, grouping separators, and decimal separators to convert localized number strings back into Elixir numeric values.
Summary
Functions
Finds and replaces substrings from a map at the beginning and/or end of a string.
Parses a string in a locale-aware manner and returns a number.
Maps a list applying a resolver function to each binary element.
Resolves currencies from strings within a list.
Resolves a currency from the beginning and/or end of a string.
Resolves percent or permille from a string.
Resolves percent and permille symbols from strings within a list.
Scans a string in a locale-aware manner and returns a list of strings and numbers.
Types
Functions
@spec find_and_replace(map(), String.t(), float() | nil) :: {:ok, list()} | {:error, Exception.t()}
Finds and replaces substrings from a map at the beginning and/or end of a string.
Arguments
string_mapis a map of%{search_string => replacement}.stringis the string to search.fuzzyis an optional float for fuzzy matching.
Returns
{:ok, list}with replacements applied.{:error, exception}if no match found.
Examples
iex> Localize.Number.Parser.find_and_replace(%{"usd" => :USD}, "USD 100")
{:ok, [:USD, " 100"]}
iex> Localize.Number.Parser.find_and_replace(%{"%" => :percent}, "11%")
{:ok, ["11", :percent]}
@spec parse(String.t(), Keyword.t()) :: {:ok, integer() | float() | Decimal.t()} | {:error, Exception.t()}
Parses a string in a locale-aware manner and returns a number.
Arguments
stringis any string.optionsis a keyword list of options.
Options
:numberis one of:integer,:float,:decimal, ornil. The default isnil(auto-detect).:localeis a locale identifier. The default is:en.:number_systemis a number system name or type.
Returns
{:ok, number}on success.{:error, exception}if parsing fails.
Examples
iex> Localize.Number.Parser.parse("-1_000_000.34")
{:ok, -1000000.34}
Maps a list applying a resolver function to each binary element.
Arguments
listis a list of terms.resolveris a function that takes a string and options.optionsis a keyword list passed to the resolver.
Options
- The options are passed through unchanged to
resolverfor each binary element. See the resolver function (for exampleresolve_currency/2orresolve_per/2) for the options it supports.
Returns
- The list with binary elements resolved.
Examples
iex> resolver = &Localize.Number.Parser.resolve_currency/2
iex> Localize.Number.Parser.resolve(["100", "USD"], resolver, locale: :en)
["100", :USD]
@spec resolve_currencies([String.t() | number()], Keyword.t()) :: [ atom() | String.t() | number() ]
Resolves currencies from strings within a list.
Arguments
listis a list of strings and numbers.optionsis a keyword list of options.
Options
:localeis a locale identifier. The default is:en.:onlyis a filter for currencies to include.:exceptis a filter for currencies to exclude.:fuzzyis a float for fuzzy matching viaString.jaro_distance/2.
Returns
- A list with currency strings replaced by currency code atoms.
Examples
iex> Localize.Number.Parser.scan("100 US dollars")
...> |> Localize.Number.Parser.resolve_currencies()
[100, :USD]
@spec resolve_currency(String.t(), Keyword.t()) :: [atom() | String.t()] | {:error, Exception.t()}
Resolves a currency from the beginning and/or end of a string.
Arguments
stringis a string potentially containing a currency name or symbol.optionsis a keyword list of options.
Options
:localeis a locale identifier. The default is:en.:onlyis a filter for currencies to include.:exceptis a filter for currencies to exclude.:fuzzyis a float for fuzzy matching.
Returns
- A list with the currency code and remainder, or
{:error, exception}.
Examples
iex> Localize.Number.Parser.resolve_currency("US dollars")
[:USD]
iex> Localize.Number.Parser.resolve_currency("$100")
[:USD, "100"]
@spec resolve_per(String.t(), Keyword.t()) :: [per() | String.t()] | {:error, Exception.t()}
Resolves percent or permille from a string.
Arguments
stringis a string potentially containing percent or permille symbols.optionsis a keyword list of options.
Options
:localeis a locale identifier atom, string, or aLocalize.LanguageTag.t/0. The default isLocalize.get_locale().:number_systemis a number system name or type atom. The default is the number system of:locale.
Returns
A list with the symbol replaced by
:percentor:permille.{:error, exception}if no symbol found.
Examples
iex> Localize.Number.Parser.resolve_per("11%")
["11", :percent]
Resolves percent and permille symbols from strings within a list.
Arguments
listis a list of strings and numbers.optionsis a keyword list of options.
Options
:localeis a locale identifier atom, string, or aLocalize.LanguageTag.t/0. The default isLocalize.get_locale().:number_systemis a number system name or type atom. The default is the number system of:locale.
Returns
- A list with percent/permille strings replaced by
:percentor:permilleatoms.
Examples
iex> Localize.Number.Parser.scan("100%")
...> |> Localize.Number.Parser.resolve_pers()
[100, :percent]
@spec scan(String.t(), Keyword.t()) :: [String.t() | integer() | float() | Decimal.t()] | {:error, Exception.t()}
Scans a string in a locale-aware manner and returns a list of strings and numbers.
Arguments
stringis any string.optionsis a keyword list of options.
Options
:numberis one of:integer,:float,:decimal, ornil. The default isnil(auto-detect).:localeis a locale identifier. The default is:en.:number_systemis a number system name or type.
Returns
- A list of strings and numbers.
Examples
iex> Localize.Number.Parser.scan("The prize is 23")
["The prize is ", 23]
iex> Localize.Number.Parser.scan("1kg")
[1, "kg"]