Units

Arithmetic for converting SI unit prefixes.

Examples

iex> Units.from(1, :mega, :byte) |> Units.to_int(:kilo, :byte)
1024
iex> Units.from(1, :mega, :byte) |> Units.to_int(:byte)
1048576
iex> Units.from(2, :kilo, :meter) |> Units.to_int(:centi, :meter)
200000

The following units of measurement are recognized:

:meter

:pico, :nano, :micro, :milli, :centi, :deci, :deca, :hecto, :kilo, :mega, :giga

:byte

:kilo, :mega, :giga, :tera, :peta

Summary

Functions

Returns the amount as a float

Creates new unit and uses a single unit of measurement

Creates new unit

Returns the amount as an integer

Converts a unit to another and returns a unit. Uses a single unit of measurement

Converts a unit to another and returns a unit

Converts a unit to another and returns a float. Short for to/2 |> float/1

Converts a unit to another and returns a float. Short for to/3 |> float/1

Converts a unit to another and returns an integer. Short for to/2 |> int/1

Converts a unit to another and returns an integer. Short for to/3 |> int/1

Functions

float(map)

Returns the amount as a float

from(amount, quantity)

Creates new unit and uses a single unit of measurement.

Example:

iex> Units.from(1, :meter)
%{amount: 1, quantity: :meter, unit: :one}
from(amount, unit, quantity)

Creates new unit.

Example:

iex> Units.from(2, :kilo, :byte)
%{amount: 2, quantity: :byte, unit: :kilo}
int(map)

Returns the amount as an integer

to(amount, quantity)

Converts a unit to another and returns a unit. Uses a single unit of measurement.

Example:

iex> Units.from(1, :kilo, :meter) |> Units.to(:meter)
%{amount: 1000, quantity: :meter, unit: :one}
to(map, dst_unit, dst_quantity)

Converts a unit to another and returns a unit.

Example:

iex> Units.from(100, :deci, :meter) |> Units.to(:centi, :meter)
%{amount: 1000, quantity: :meter, unit: :centi}
to_float(amount, quantity)

Converts a unit to another and returns a float. Short for to/2 |> float/1

to_float(amount, dst_unit, quantity)

Converts a unit to another and returns a float. Short for to/3 |> float/1

to_int(amount, quantity)

Converts a unit to another and returns an integer. Short for to/2 |> int/1

to_int(amount, dst_unit, quantity)

Converts a unit to another and returns an integer. Short for to/3 |> int/1