Cldr.Unit.Conversion (Cldr Units v3.3.0) View Source
Unit conversion functions for the units defined
in Cldr.
Link to this section Summary
Functions
Convert one unit into another unit of the same unit type (length, volume, mass, ...)
Convert one unit into another unit of the same unit type (length, volume, mass, ...) and raises on a unit type mismatch
Convert a unit into its base unit.
Convert a unit into its base unit and raises on error
Link to this section Types
Specs
Link to this section Functions
Specs
convert(Cldr.Unit.t(), Cldr.Unit.unit()) :: {:ok, Cldr.Unit.t()} | {:error, {module(), String.t()}}
Convert one unit into another unit of the same unit type (length, volume, mass, ...)
Arguments
unitis any unit returned byCldr.Unit.new/2to_unitis any unit name returned byCldr.Unit.units/0
Returns
a
Unit.tof the unit typeto_unitor{:error, {exception, message}}
Examples
iex> Cldr.Unit.convert Cldr.Unit.new!(:mile, 1), :foot
{:ok, Cldr.Unit.new!(:foot, 5280)}
iex> Cldr.Unit.convert Cldr.Unit.new!(:mile, 1), :gallon
{:error, {Cldr.Unit.IncompatibleUnitsError,
"Operations can only be performed between units of the same category. Received :mile and :gallon"}}
Specs
convert!(Cldr.Unit.t(), Cldr.Unit.unit()) :: Cldr.Unit.t() | no_return()
Convert one unit into another unit of the same unit type (length, volume, mass, ...) and raises on a unit type mismatch
Arguments
unitis any unit returned byCldr.Unit.new/2to_unitis any unit name returned byCldr.Unit.units/0
Returns
a
Unit.tof the unit typeto_unitorraises an exception
Examples
iex> Cldr.Unit.Conversion.convert!(Cldr.Unit.new!(:celsius, 0), :fahrenheit)
...> |> Cldr.Unit.round
#Cldr.Unit<:fahrenheit, 32.0>
iex> Cldr.Unit.Conversion.convert!(Cldr.Unit.new!(:fahrenheit, 32), :celsius)
...> |> Cldr.Unit.round
#Cldr.Unit<:celsius, 0.0>
Cldr.Unit.Conversion.convert Cldr.Unit.new!(:mile, 1), :gallon
** (Cldr.Unit.IncompatibleUnitsError) Operations can only be performed between units of the same type. Received :mile and :gallon
Convert a unit into its base unit.
For example, the base unit for length
is meter. The base unit is an
intermediary unit used in all
conversions.
Arguments
unitis any unit returned byCldr.Unit.new/2
Returns
unitconverted to its base unit as at:Unit.t()or{;error, {exception, reason}}as an error
Example
iex> unit = Cldr.Unit.new!(:kilometer, 10)
iex> Cldr.Unit.Conversion.convert_to_base_unit unit
{:ok, Cldr.Unit.new!(:meter, 10000)}
Convert a unit into its base unit and raises on error
For example, the base unit for length
is meter. The base unit is an
intermediary unit used in all
conversions.
Arguments
unitis any unit returned byCldr.Unit.new/2
Returns
unitconverted to its base unit as at:Unit.t()orraises an exception
Example
iex> unit = Cldr.Unit.new!(:kilometer, 10)
iex> Cldr.Unit.Conversion.convert_to_base_unit! unit
#Cldr.Unit<:meter, 10000>