Cldr.Unit.parse
parse, go back to Cldr.Unit module for more information.
Parse a string to create a new unit.
This function attempts to parse a string
into a number and unit type. If successful
it attempts to create a new unit using
Cldr.Unit.new/3.
The parsed unit type is aliased against all the
known unit names for a give locale (or the current
locale if no locale is specified). The known
aliases for unit types can be returned with
MyApp.Cldr.Unit.unit_strings_for/1 where MyApp.Cldr
is the name of a backend module.
Arguments
unit stringis any string to be parsed and if possible used to create a newt:Cldr.Unitoptionsis a keyword list of options
Options
:localeis any valid locale name returned byCldr.known_locale_names/0or at:Cldr.LanguageTagstruct. The default isCldr.get_locale/0:backendis any module that includesuse Cldrand therefore is aCldrbackend module. The default isCldr.default_backend!/0.
Returns
{:ok, unit}or{:error, {exception, reason}}
Examples
iex> Cldr.Unit.parse "1kg"
Cldr.Unit.new(1, :kilogram)
iex> Cldr.Unit.parse "1 tages", locale: "de"
Cldr.Unit.new(1, :day)
iex> Cldr.Unit.parse "1 tag", locale: "de"
Cldr.Unit.new(1, :day)
iex> Cldr.Unit.parse("42 millispangels")
{:error, {Cldr.UnknownUnitError, "Unknown unit was detected at \"spangels\""}}