MyApp.Cldr.Unit.to_string

You're seeing just the function to_string, go back to MyApp.Cldr.Unit module for more information.
Link to this function

to_string(number, options \\ [])

View Source

Specs

to_string(Cldr.Unit.value() | 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 a t:Cldr.Unit struct or a list of t:Cldr.Unit structs

  • options is a keyword list

Options

  • :unit is any unit returned by Cldr.Unit.known_units/0. Ignored if the number to be formatted is a t:Cldr.Unit struct

  • :locale is any valid locale name returned by Cldr.known_locale_names/0 or a Cldr.LanguageTag struct. The default is Cldr.get_locale/0

  • :style is one of those returned by Cldr.Unit.available_styles. The current styles are :long, :short and :narrow. The default is style: :long

  • :grammatical_case indicates that a localisation for the given locale and given grammatical case should be used. See Cldr.Unit.known_grammatical_cases/0 for the list of known grammatical cases. Note that not all locales define all cases. However all locales do define the :nominative case, which is also the default.

  • :gender indicates that a localisation for the given locale and given grammatical gender should be used. See Cldr.Unit.known_gender/0 for the list of known grammatical genders. Note that not all locales define all genders. The default gender is Cldr.Unit.default_gender/1 for the given locale.

  • :list_options is a keyword list of options for formatting a list which is passed through to Cldr.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 the number

Returns

  • {:ok, formatted_string} or

  • {:error, {exception, message}}

Examples

iex> MyApp.Cldr.Unit.to_string Cldr.Unit.new!(:gallon, 123)
{:ok, "123 gallons"}

iex> MyApp.Cldr.Unit.to_string Cldr.Unit.new!(:gallon, 1)
{:ok, "1 gallon"}

iex> MyApp.Cldr.Unit.to_string Cldr.Unit.new!(:gallon, 1), locale: "af"
{:ok, "1 gelling"}

iex> MyApp.Cldr.Unit.to_string Cldr.Unit.new!(:gallon, 1), locale: "af-NA"
{:ok, "1 gelling"}

iex> MyApp.Cldr.Unit.to_string Cldr.Unit.new!(:gallon, 1), locale: "bs"
{:ok, "1 galon"}

iex> MyApp.Cldr.Unit.to_string Cldr.Unit.new!(:gallon, 1234), format: :long
{:ok, "1 thousand gallons"}

iex> MyApp.Cldr.Unit.to_string Cldr.Unit.new!(:gallon, 1234), format: :short
{:ok, "1K gallons"}

iex> MyApp.Cldr.Unit.to_string Cldr.Unit.new!(:megahertz, 1234)
{:ok, "1,234 megahertz"}

iex> MyApp.Cldr.Unit.to_string Cldr.Unit.new!(:megahertz, 1234), style: :narrow
{:ok, "1,234MHz"}

iex> MyApp.Cldr.Unit.to_string Cldr.Unit.new!(:megabyte, 1234), locale: "en", style: :unknown
{:error, {Cldr.UnknownFormatError, "The unit style :unknown is not known."}}