View Source Quantity (Quantity v1.0.0)
A data structure that encapsulates a decimal value with a unit.
Link to this section Summary
Functions
Extracts the base value from the quantity
Compares two quantities with the same unit numerically
Converts the quantity to have a new unit. The new unit must be a whole 10-exponent more or less than the original unit.
Encodes a decimal as string. Uses either :raw (E-notation) or :normal based on exponent, so that precision is not lost
Returns true if the two quantities are numerically equal
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
Same as parse/1, but raises if it could not parse
Parses a string representation of a quantity (perhaps generated with to_string/1)
Test whether a Quantity is positive
Reduces the value to the largest possible exponent without altering the numerical value
Converts a 1-unit quantity to a decimal. If the quantity does not represent a decimal (a unit other than 1) it fails.
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
Return a quantity with a zero value and the same unit and precision as another Quantity
Tries to create a new Quantity. If it fails because of infinity or NaN decimal, will return an error tuple. This could be used instead of new/2 when creating a Quantity from user input or other thirdparty input.
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.abs/1
.
See Quantity.Math.add!/2
.
See Quantity.Math.add/2
.
Extracts the base value from the quantity
Compares two quantities with the same unit numerically
iex> Quantity.compare(~Q[1.00 m], ~Q[2.00 m]) :lt iex> Quantity.compare(~Q[1.00 m], ~Q[1 m]) :eq iex> Quantity.compare(~Q[3.00 m], ~Q[2.9999999 m]) :gt
Converts the quantity to have a new unit. The new unit must be a whole 10-exponent more or less than the original unit.
The exponent given is the difference in exponents (new-exponent - old-exponent). For example when converting from kWh to MWh: 6 (MWh) - 3 (kWh) = 3
iex> ~Q[1234E3 Wh] |> Quantity.convert_unit("MWh", 6) ~Q[1.234 MWh]
iex> ~Q[25.2 m] |> Quantity.convert_unit("mm", -3) ~Q[252E2 mm]
Encodes a decimal as string. Uses either :raw (E-notation) or :normal based on exponent, so that precision is not lost
iex> Quantity.decimal_to_string(~d[1.234]) "1.234"
iex> Quantity.decimal_to_string(~d[1E3]) "1E3"
See Quantity.Math.div/2
.
Returns true if the two quantities are numerically equal
iex> Quantity.equals?(~Q[5 bananas], ~Q[5.0 bananas]) true
iex> Quantity.equals?(~Q[5 bananas], ~Q[5 apples]) false
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
Same as parse/1, but raises if it could not parse
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
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
Reduces the value to the largest possible exponent without altering the numerical value
iex> Quantity.reduce(~Q[1.200 m]) ~Q[1.2 m]
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
.
Converts a 1-unit quantity to a decimal. If the quantity does not represent a decimal (a unit other than 1) it fails.
iex> Quantity.to_decimal!(~Q[42]) ~d[42]
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"
Return a quantity with a zero value and the same unit and precision as another Quantity
iex> ~Q[123.99 EUR] |> Quantity.to_zero() ~Q[0.00 EUR]
iex> ~Q[1 person] |> Quantity.to_zero() ~Q[0 person]
iex> ~Q[-123 seconds] |> Quantity.to_zero() ~Q[0 seconds]
Tries to create a new Quantity. If it fails because of infinity or NaN decimal, will return an error tuple. This could be used instead of new/2 when creating a Quantity from user input or other thirdparty input.
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