Cldr Units v2.1.0 TestBackend.Cldr.Unit View Source
Supports the CLDR Units definitions which provide for the localization of many unit types.
Link to this section Summary
Functions
Formats a list using to_string/3
but raises if there is
an error
Formats a number into a string according to a unit definition for a locale
Link to this section Functions
Formats a list using to_string/3
but raises if there is
an error.
Arguments
list_or_number
is any number (integer, float or Decimal) or aCldr.Unit.t()
struct or a list ofCldr.Unit.t()
structsoptions
is a keyword list
Options
:unit
is any unit returned byCldr.Unit.units/2
. Ignored if the number to be formatted is aCldr.Unit.t()
struct:locale
is any valid locale name returned byCldr.known_locale_names/0
or aCldr.LanguageTag
struct. The default isCldr.get_current_locale/0
:style
is one of those returned byCldr.Unit.available_styles
. The current styles are:long
,:short
and:narrow
. The default isstyle: :long
:list_options
is a keyword list of options for formatting a list which is passed through toCldr.List.to_string/3
. This is only applicable when formatting a list of units.Any other options are passed to
Cldr.Number.to_string/2
which is used to format thenumber
Returns
formatted_string
orraises and exception
Examples
iex> TestBackend.Cldr.Unit.to_string! 123, unit: :gallon
"123 gallons"
iex> TestBackend.Cldr.Unit.to_string! 1, unit: :gallon
"1 gallon"
iex> TestBackend.Cldr.Unit.to_string! 1, unit: :gallon, locale: "af"
"1 gelling"
to_string( Cldr.Math.number_or_decimal() | Cldr.Unit.t() | [Cldr.Unit.t(), ...], Keyword.t() ) :: {:ok, String.t()} | {:error, {atom(), binary()}}
Formats a number into a string according to a unit definition for a locale.
Arguments
list_or_number
is any number (integer, float or Decimal) or aCldr.Unit.t()
struct or a list ofCldr.Unit.t()
structsoptions
is a keyword list
Options
:unit
is any unit returned byCldr.Unit.units/2
. Ignored if the number to be formatted is aCldr.Unit.t()
struct:locale
is any valid locale name returned byCldr.known_locale_names/0
or aCldr.LanguageTag
struct. The default isCldr.get_current_locale/0
:style
is one of those returned byCldr.Unit.available_styles
. The current styles are:long
,:short
and:narrow
. The default isstyle: :long
:list_options
is a keyword list of options for formatting a list which is passed through toCldr.List.to_string/3
. This is only applicable when formatting a list of units.Any other options are passed to
Cldr.Number.to_string/2
which is used to format thenumber
Returns
{:ok, formatted_string}
or{:error, {exception, message}}
Examples
iex> TestBackend.Cldr.Unit.to_string 123, unit: :gallon
{:ok, "123 gallons"}
iex> TestBackend.Cldr.Unit.to_string 1, unit: :gallon
{:ok, "1 gallon"}
iex> TestBackend.Cldr.Unit.to_string 1, unit: :gallon, locale: "af"
{:ok, "1 gelling"}
iex> TestBackend.Cldr.Unit.to_string 1, unit: :gallon, locale: "af-NA"
{:ok, "1 gelling"}
iex> TestBackend.Cldr.Unit.to_string 1, unit: :gallon, locale: "bs"
{:ok, "1 galon"}
iex> TestBackend.Cldr.Unit.to_string 1234, unit: :gallon, format: :long
{:ok, "1 thousand gallons"}
iex> TestBackend.Cldr.Unit.to_string 1234, unit: :gallon, format: :short
{:ok, "1K gallons"}
iex> TestBackend.Cldr.Unit.to_string 1234, unit: :megahertz
{:ok, "1,234 megahertz"}
iex> TestBackend.Cldr.Unit.to_string 1234, unit: :megahertz, style: :narrow
{:ok, "1,234MHz"}
iex> TestBackend.Cldr.Unit.to_string 123, unit: :megabyte, locale: "en", style: :unknown
{:error, {Cldr.UnknownFormatError, "The unit style :unknown is not known."}}
iex> TestBackend.Cldr.Unit.to_string 123, unit: :blabber, locale: "en"
{:error, {Cldr.UnknownUnitError, "The unit :blabber is not known."}}