Cldr_Units v0.1.3 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 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_units(locale \\ Cldr.get_current_locale(), style \\ :long) View Source

Returns the available units 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

Cldr.Unit.available_units
[:acceleration_g_force, :acceleration_meter_per_second_squared,
 :angle_arc_minute, :angle_arc_second, :angle_degree, :angle_radian,
 :angle_revolution, :area_acre, :area_hectare, :area_square_centimeter,
 :area_square_foot, :area_square_inch, :area_square_kilometer,
 :area_square_meter, :area_square_mile, :area_square_yard, :concentr_karat,
 :concentr_milligram_per_deciliter, :concentr_millimole_per_liter,
 :concentr_part_per_million, :consumption_liter_per_100kilometers,
 :consumption_liter_per_kilometer, :consumption_mile_per_gallon,
 :consumption_mile_per_gallon_imperial, :coordinate_unit, :digital_bit,
 :digital_byte, :digital_gigabit, :digital_gigabyte, :digital_kilobit,
 :digital_kilobyte, :digital_megabit, :digital_megabyte, :digital_terabit,
 :digital_terabyte, :duration_century, :duration_day, :duration_hour,
 :duration_microsecond, :duration_millisecond, :duration_minute,
 :duration_month, :duration_nanosecond, :duration_second, :duration_week,
 :duration_year, :electric_ampere, :electric_milliampere, :electric_ohm,
 :electric_volt, ...]
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.

  • number is any number (integer, float or Decimal)

  • unit is any unit returned by Cldr.Unit.available_units/0

  • options are:

    • locale is any configured locale. See Cldr.known_locales(). The default is locale: Cldr.get_currenct_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

    • Any other options are passed to Cldr.Number.to_string/2 which is used to format the number

Examples

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

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

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

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

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

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

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

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

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

Cldr.Unit.to_string 123, :digital_megabyte, locale: "en-XX"
{:error, {Cldr.UnknownLocaleError, "The locale "en-XX" is not known."}}

Cldr.Unit.to_string 123, :digital_megabyte, locale: "en", style: :unknown
{:error, {Cldr.UnknownFormatError, "The unit style :unknown 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, :volume_gallon
"123 gallons"

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

iex> Cldr.Unit.to_string! 1, :volume_gallon, locale: "af"
"1 gelling"