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 section Functions
See Quantity.Math.add/2
.
See Quantity.Math.add!/2
.
Extracts the base value from the quantity
See Quantity.Math.div/2
.
Extracts the exponent from the quantity
See Quantity.Math.mult/2
.
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
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)
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
Same as parse/1, but raises if it could not parse
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
See Quantity.Math.sub/2
.
See Quantity.Math.sub!/2
.
See Quantity.Math.sum/1
.
See Quantity.Math.sum/3
.
See Quantity.Math.sum!/1
.
See Quantity.Math.sum!/3
.
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"
Extracts the unit from the quantity
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