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.
@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.
Returns
- The list with binary elements resolved.
@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.
@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}.
@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.
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.
Returns
- A list with percent/permille strings replaced by
:percentor:permilleatoms.
@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"]