View Source Cldr.Unit.Range (Cldr Units v3.17.0)

Implements a unit range type, similar to Date.Range.

A unit range but be comprise first and last units that have the same base unit. The last unit will be converted to the unit type of the first unit.

Unit ranges are useful in at least the following cases:

  1. To be able to enumerate a unit range.
  2. To be able to format a unit range in a localised manner using Cldr.Number.to_range_string/2.

Summary

Types

t()

A unit range consists of two units where the last unit can be converted to the same unit type as the first unit.

Types

@type t() :: %{first: Cldr.Unit.t(), last: Cldr.Unit.t()}

A unit range consists of two units where the last unit can be converted to the same unit type as the first unit.

Functions

Link to this function

new(first, last)

View Source (since 3.16.0)
@spec new(Cldr.Unit.t(), Cldr.Unit.t()) ::
  {:ok, t()} | {:error, {module(), String.t()}}

Returns a new Cldr.Unit.Range.

Arguments

Returns

  • {:ok, unit_range} or

  • {:error, {exception, reason}}.

Examples

iex> Cldr.Unit.Range.new Cldr.Unit.new!(:gram, 1), Cldr.Unit.new!(:gram, 4)
{:ok, Cldr.Unit.Range.new!(Cldr.Unit.new!(:gram, 1), Cldr.Unit.new!(:gram, 4))}

iex> Cldr.Unit.Range.new Cldr.Unit.new!(:gram, 1), Cldr.Unit.new!(:liter, 4)
{:error,
 {Cldr.Unit.InvalidRangeError,
  "Unit ranges require that the last unit can be converted to the first unit. " <>
  "Found Cldr.Unit.new!(:gram, 1) and Cldr.Unit.new!(:liter, 4)"}}

iex> Cldr.Unit.Range.new Cldr.Unit.new!(:gram, 5), Cldr.Unit.new!(:gram, 4)
{:error,
 {Cldr.Unit.InvalidRangeError,
  "Unit ranges require that the first unit be less than or equal to the last. " <>
  "Found Cldr.Unit.new!(:gram, 5) and Cldr.Unit.new!(:gram, 4)"}}
Link to this function

new!(first, last)

View Source (since 3.16.0)
@spec new!(Cldr.Unit.t(), Cldr.Unit.t()) :: t() | no_return()

Returns a new Cldr.Unit.Range or raises an exception.

Arguments

Returns

  • unit_range or

  • raises an exception.

Example

iex> Cldr.Unit.Range.new! Cldr.Unit.new!(:gram, 1), Cldr.Unit.new!(:gram, 4)
Cldr.Unit.Range.new!(Cldr.Unit.new!(:gram, 1), Cldr.Unit.new!(:gram, 4))

See Cldr.Unit.Format.to_iolist/1.

Link to this function

to_iolist(range, options)

View Source

See Cldr.Unit.Format.to_iolist/2.

Link to this function

to_iolist(range, backend, options)

View Source

See Cldr.Unit.Format.to_iolist/3.

See Cldr.Unit.Format.to_string/1.

Link to this function

to_string(range, options)

View Source

See Cldr.Unit.Format.to_string/2.

Link to this function

to_string(range, backend, options)

View Source

See Cldr.Unit.Format.to_string/3.