Quantity v0.3.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

Test whether a Quantity is negative

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 raises if it could not parse

Test whether a Quantity is positive

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

Tests if a quantity has zero value

Link to this section Types

Link to this type

t()

View Source
t() :: %Quantity{unit: unit(), value: Decimal.t()}
Link to this type

unit()

View Source
unit() :: String.t() | {:div | :mult, String.t(), String.t()}

Link to this section Functions

Link to this function

add(quantity_1, quantity_2)

View Source

See Quantity.Math.add/2.

Link to this function

add!(quantity_1, quantity_2)

View Source

See Quantity.Math.add!/2.

Link to this function

base_value(quantity)

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

Extracts the base value from the quantity

See Quantity.Math.div/2.

Link to this function

exponent(quantity)

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

Extracts the exponent from the quantity

Link to this function

mult(quantity, quantity_or_scalar)

View Source

See Quantity.Math.mult/2.

Link to this function

negative?(map)

View Source
negative?(t()) :: boolean()

Test whether a Quantity is negative

iex> ~Q[100.00 DKK] |> Quantity.negative?() false

iex> ~Q[0.00 DKK] |> Quantity.negative?() false

iex> ~Q[-1.93 DKK] |> Quantity.negative?() true

Link to this function

new(value, unit)

View Source
new(Decimal.t(), unit()) :: t()

Builds a new Quantity from a Decimal and a unit

Link to this function

new(base_value, exponent, unit)

View Source
new(integer(), integer(), unit()) :: 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)

iex> Quantity.parse("99.0 red_balloons")

iex> Quantity.parse("15 bananas/monkey") {:ok, Quantity.new(~d[15], {:div, "bananas", "monkey"})}

iex> Quantity.parse("15 m*m") {:ok, Quantity.new(~d[15], {:mult, "m", "m"})}

iex> Quantity.parse("bogus") :error

Link to this function

parse!(input)

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

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

Link to this function

positive?(map)

View Source
positive?(t()) :: boolean()

Test whether a Quantity is positive

iex> ~Q[100.00 DKK] |> Quantity.positive?() true

iex> ~Q[0.00 DKK] |> Quantity.positive?() false

iex> ~Q[-1.93 DKK] |> Quantity.positive?() false

Link to this function

round(quantity, decimals)

View Source

See Quantity.Math.round/2.

Link to this function

sub(quantity_1, quantity_2)

View Source

See Quantity.Math.sub/2.

Link to this function

sub!(quantity_1, quantity_2)

View Source

See Quantity.Math.sub!/2.

See Quantity.Math.sum/1.

Link to this function

sum(quantities, exp, unit)

View Source

See Quantity.Math.sum/3.

See Quantity.Math.sum!/1.

Link to this function

sum!(quantities, exp, unit)

View Source

See Quantity.Math.sum!/3.

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" iex> Quantity.new(~d[3600], {:div, "seconds", "hour"}) |> Quantity.to_string() "3600 seconds/hour" iex> Quantity.new(~d[34], {:mult, "m", "m"}) |> Quantity.to_string() "34 m*m"

Link to this function

unit(quantity)

View Source
unit(t()) :: unit()

Extracts the unit from the quantity

Link to this function

zero?(quantity)

View Source
zero?(t()) :: boolean()

Tests if a quantity has zero value

iex> Quantity.zero?(~Q[0.00 m^2]) true

iex> Quantity.zero?(~Q[0E7 m^2]) true

iex> Quantity.zero?(~Q[10 m^2]) false