Quantity v0.1.0 Quantity View Source

A data structure that encapsulates a decimal value with a unit.

Link to this section Summary

Functions

Extracts the base value from the quantity

Extracts the exponent from the quantity

Builds a new Quantity from a Decimal and a unit

Builds a new Quantity from a base value, exponent and unit

Parses a string representation of a quantity (perhaps generated with to_string/1)

Same as parse/1, but errors if it could not parse

Encodes the quantity as a string. The result is parsable with parse/1 If the exponent is positive, encode usinge the "raw" format to preserve precision

Extracts the unit from the quantity

Link to this section Types

Link to this type

t()

View Source
t() :: %Quantity{unit: String.t(), value: Decimal.t()}

Link to this section Functions

Link to this function

base_value(quantity)

View Source
base_value(t()) :: integer()

Extracts the base value from the quantity

Link to this function

exponent(quantity)

View Source
exponent(t()) :: integer()

Extracts the exponent from the quantity

Builds a new Quantity from a Decimal and a unit

Link to this function

new(base_value, exponent, unit)

View Source
new(integer(), integer(), String.t()) :: t()

Builds a new Quantity from a base value, exponent and unit

Link to this function

parse(input)

View Source
parse(String.t()) :: {:ok, t()} | :error

Parses a string representation of a quantity (perhaps generated with to_string/1)

Link to this function

parse!(input)

View Source
parse!(String.t()) :: t()

Same as parse/1, but errors if it could not parse

Link to this function

to_string(quantity)

View Source
to_string(t()) :: String.t()

Encodes the quantity as a string. The result is parsable with parse/1 If the exponent is positive, encode usinge the "raw" format to preserve precision

iex> Quantity.new(42, -1, "db") |> Quantity.to_string() "4.2 db" iex> Quantity.new(42, 1, "db") |> Quantity.to_string() "42E1 db"

Extracts the unit from the quantity