Cldr Units v2.8.0 Cldr.Unit.Conversion 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
Link to this section Functions
convert(from, to_unit)
View Sourceconvert(Cldr.Unit.t(), Cldr.Unit.unit()) :: Cldr.Unit.t() | {:error, {module(), String.t()}}
Convert one unit into another unit of the same unit type (length, volume, mass, ...)
Options
unit
is any unit returned byCldr.Unit.new/2
to_unit
is any unit name returned byCldr.Unit.units/0
Returns
a
Unit.t
of the unit typeto_unit
or{:error, {exception, message}}
Examples
iex> Cldr.Unit.convert Cldr.Unit.new!(:celsius, 0), :fahrenheit
#Unit<:fahrenheit, 32.0>
iex> Cldr.Unit.convert Cldr.Unit.new!(:fahrenheit, 32), :celsius
#Unit<:celsius, 0.0>
iex> Cldr.Unit.convert Cldr.Unit.new!(:mile, 1), :foot
#Unit<:foot, 5279.945925937846>
iex> Cldr.Unit.convert Cldr.Unit.new!(:mile, 1), :gallon
{:error, {Cldr.Unit.IncompatibleUnitsError,
"Operations can only be performed between units of the same type. Received :mile and :gallon"}}
convert!(unit, to_unit)
View Sourceconvert!(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
Options
unit
is any unit returned byCldr.Unit.new/2
to_unit
is any unit name returned byCldr.Unit.units/0
Returns
a
Unit.t
of the unit typeto_unit
orraises an exception
Examples
iex> Cldr.Unit.Conversion.convert! Cldr.Unit.new!(:celsius, 0), :fahrenheit
#Unit<:fahrenheit, 32.0>
iex> Cldr.Unit.Conversion.convert! Cldr.Unit.new!(:fahrenheit, 32), :celsius
#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