Cldr_Units v0.4.4 Cldr.Unit View Source

Supports the CLDR Units definitions which provide for the localization of many unit types.

The public API defines two primary functions:

Link to this section Summary

Functions

Returns the available styles for a unit localiation

Returns the available unit types for a given locale and style

Returns the available units for a given locale and style

Formats a number into a string according to a unit definition for a locale

Formats a list using to_string/3 but raises if there is an error

Link to this section Functions

Returns the available styles for a unit localiation.

Example

iex> Cldr.Unit.available_styles
[:long, :short, :narrow]
Link to this function available_unit_types(locale \\ Cldr.get_current_locale(), style \\ :long) View Source

Returns the available unit types for a given locale and style.

  • locale is any configured locale. See Cldr.known_locales(). The default is locale: Cldr.get_current_locale()

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

Example

iex> Cldr.Unit.available_unit_types
[:acceleration, :angle, :area, :concentr, :consumption, :coordinate, :digital,
 :duration, :electric, :energy, :frequency, :length, :light, :mass, :power,
 :pressure, :speed, :temperature, :volume]
Link to this function available_units(locale \\ Cldr.get_current_locale(), style \\ :long) View Source

Returns the available units for a given locale and style.

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

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

Example

Cldr.Unit.available_units
[:acre, :acre_foot, :ampere, :arc_minute, :arc_second, :astronomical_unit, :bit,
 :bushel, :byte, :calorie, :carat, :celsius, :centiliter, :centimeter, :century,
 :cubic_centimeter, :cubic_foot, :cubic_inch, :cubic_kilometer, :cubic_meter,
 :cubic_mile, :cubic_yard, :cup, :cup_metric, :day, :deciliter, :decimeter,
 :degree, :fahrenheit, :fathom, :fluid_ounce, :foodcalorie, :foot, :furlong,
 :g_force, :gallon, :gallon_imperial, :generic, :gigabit, :gigabyte, :gigahertz,
 :gigawatt, :gram, :hectare, :hectoliter, :hectopascal, :hertz, :horsepower,
 :hour, :inch, ...]
Link to this function to_string(number, unit, options \\ []) View Source
to_string(Cldr.Math.number_or_decimal(), atom(), Keyword.t()) ::
  {:ok, String.t()} |
  {:error, {atom(), binary()}}

Formats a number into a string according to a unit definition for a locale.

Examples

iex> Cldr.Unit.to_string 123, :gallon
{:ok, "123 gallons"}

iex> Cldr.Unit.to_string 1, :gallon
{:ok, "1 gallon"}

iex> Cldr.Unit.to_string 1, :gallon, locale: "af"
{:ok, "1 gelling"}

iex> Cldr.Unit.to_string 1, :gallon, locale: "af-NA"
{:ok, "1 gelling"}

iex> Cldr.Unit.to_string 1, :gallon, locale: "bs"
{:ok, "1 galona"}

iex> Cldr.Unit.to_string 1234, :gallon, format: :long
{:ok, "1 thousand gallons"}

iex> Cldr.Unit.to_string 1234, :gallon, format: :short
{:ok, "1K gallons"}

iex> Cldr.Unit.to_string 1234, :megahertz
{:ok, "1,234 megahertz"}

iex> Cldr.Unit.to_string 1234, :megahertz, style: :narrow
{:ok, "1,234MHz"}

iex> Cldr.Unit.to_string 123, :megabyte, locale: "en", style: :unknown
{:error, {Cldr.UnknownFormatError, "The unit style :unknown is not known."}}

iex> Cldr.Unit.to_string 123, :blabber, locale: "en"
{:error, {Cldr.UnknownUnitError, "The unit :blabber is not known."}}
Link to this function to_string!(number, unit, options \\ []) View Source
to_string!(List.t(), atom(), Keyword.t()) ::
  String.t() |
  Exception.t()

Formats a list using to_string/3 but raises if there is an error.

Examples

iex> Cldr.Unit.to_string! 123, :gallon
"123 gallons"

iex> Cldr.Unit.to_string! 1, :gallon
"1 gallon"

iex> Cldr.Unit.to_string! 1, :gallon, locale: "af"
"1 gelling"
Link to this function units_for(locale \\ Cldr.get_current_locale(), style \\ :long) View Source