Create a quantity from an expression, a scalar and units, or a parsed definition.
A full expression such as "3 m", a scalar paired with
initUnits, or a QuantityDefinition / existing Quantity.
Nonempty unit expression for a separate scalar. A scalar in
this expression is replaced by initValue. Ignored for quantity definitions.
Parser used for string expressions; supplied by the configured entry point. Required even for definitions; retained for subsequent string operations.
If inputs cannot be parsed, an absolute temperature is below absolute zero, or absolute-temperature units occur in a compound expression.
Create a new quantity with the same value and units.
A distinct Quantity instance with its own conversion cache.
StaticwithCreate a class with isolated numerical settings and optional cache limits.
Decimal overrides, parser settings and conversionCache limits, merged with this class's settings.
A new class whose instances and derived results retain these settings.
Lazily calculated numerical value in base units; absolute temperatures use kelvin. Recomputed on access when the shared Decimal configuration changes.
toBase for a quantity with base units and this value.
The original scalar/string input, or a normalized value-and-units snapshot for Quantity and definition inputs. Numeric toString() objects are saved as their parsed Decimal scalar. Copies do not retain the source object.
Numerical value in this quantity's units, stored as @neutrium/decimal Decimal.
Use scalar.toString() to retain decimal digits, or scalar.toNumber() when a
JavaScript number is needed and floating-point rounding is acceptable.
Read-only; create a new quantity to change the value.
Cached dimensional signature. Prefer isCompatible over interpreting this string.
Format the counted units as a unit expression.
The expression without its scalar; an empty string for a unitless quantity.
Convert to compatible units, or reciprocal units by inverting the quantity.
Target unit expression or Quantity. Only the units are used; any target scalar is ignored, including zero or negative values.
The quantity expressed in the target units. Empty target strings and unchanged units return this instance; repeated conversions can return cached objects.
If parsing fails, dimensions are neither compatible nor reciprocal, or reciprocal conversion requires inverting a zero value or absolute temperature.
const mass = new Quantity('25 kg');
mass.to('g').scalar.toString(); // "25000"
mass.to(new Quantity('3 g')).scalar.toString(); // "25000"
new Quantity('2 m').to('m^-1').scalar.toString(); // "0.5"
Convert this quantity to the library's base unit system.
A quantity in base units, or this instance if it already uses base units.
Absolute temperatures are converted to tempK with the appropriate offset.
Add a quantity after converting it to compatible units.
A quantity expression, definition, Quantity, or scalar input. Scalars without units are dimensionless and require a compatible receiver.
A new quantity, normally in this quantity's units. Adding degrees to an absolute temperature returns an absolute temperature.
Divide by a scalar or combine units with another quantity.
A number, Decimal, numeric toString() object, quantity expression, definition, or Quantity.
A new quotient. Divisors without unit tokens preserve the current units, including numeric strings and dimensionless Quantity instances. Compatible quantities are converted before division, except for temperature degrees.
Take the reciprocal of the scalar and swap numerator and denominator units.
A new reciprocal quantity.
Multiply by a scalar or combine units with another quantity.
A number, Decimal, numeric toString() object, quantity expression, definition, or Quantity.
A new product. Scalars and quantities without unit tokens preserve the other operand's units. Compatible quantities are converted to the left operand's units before multiplication, except for temperature degrees.
Raise the scalar and units to an integer power.
Integer exponent as a number, decimal string, or Decimal. Negative exponents invert the unit expression.
A new quantity with the powered scalar and units.
If the exponent is fractional or invalid, or the resulting units violate absolute-temperature restrictions. Exponents and resulting counts outside the safe-integer range throw RangeError.
Subtract a quantity after converting it to compatible units.
A quantity expression, definition, Quantity, or scalar input. Scalars without units are dimensionless and require a compatible receiver.
A new quantity in this quantity's units, except that subtracting two absolute temperatures returns temperature degrees.
Compare compatible physical values, or a numeric value in the current units.
Quantity or quantity expression, or a number/Decimal in this quantity's current units. Numeric operands require no parsing or temporary Quantity.
-1 if this quantity is smaller, 0 if equal, or 1 if larger;
undefined when either value is NaN. Boolean comparisons return false for NaN.
Test whether this physical value is equal to another compatible value.
Quantity or expression, or a number/Decimal in this quantity's current units.
Whether the comparison holds. Numeric operands compare directly with the scalar; Quantity and string operands compare compatible physical values. NaN returns false.
compareTo for three-way comparison.
Test whether this physical value is greater than another compatible value.
Quantity or expression, or a number/Decimal in this quantity's current units.
Whether the comparison holds. Numeric operands compare directly with the scalar; Quantity and string operands compare compatible physical values. NaN returns false.
compareTo for three-way comparison.
Test whether this physical value is greater than or equal to another compatible value.
Quantity or expression, or a number/Decimal in this quantity's current units.
Whether the comparison holds. Numeric operands compare directly with the scalar; Quantity and string operands compare compatible physical values. NaN returns false.
compareTo for three-way comparison.
Test whether two quantities have the same dimensional signature.
A Quantity or unit expression; numeric inputs return false.
Whether dimensions match, without comparing scalars or converting units. Absolute temperatures and temperature degrees share a signature.
Test whether another quantity has reciprocal dimensions.
A Quantity or expression describing the reciprocal units.
Whether all dimensional exponents are the negatives of those in b.
Scalars are ignored, including zero. Absolute temperatures and temperature
degrees share a dimension; this check does not imply that inversion is allowed.
Test whether this physical value is less than another compatible value.
Quantity or expression, or a number/Decimal in this quantity's current units.
Whether the comparison holds. Numeric operands compare directly with the scalar; Quantity and string operands compare compatible physical values. NaN returns false.
compareTo for three-way comparison.
Test whether this physical value is less than or equal to another compatible value.
Quantity or expression, or a number/Decimal in this quantity's current units.
Whether the comparison holds. Numeric operands compare directly with the scalar; Quantity and string operands compare compatible physical values. NaN returns false.
compareTo for three-way comparison.
Test exact scalar equality and matching normalized unit records.
Quantity to compare with this instance.
Whether scalars and ordered unit/prefix/exponent records match. Different compatible units return false even when they represent the same physical value.
Test for a standalone, unprefixed temperature unit, including absolute temperatures.
True for both degC-style intervals and tempC-style absolute temperatures.
To identify an unprefixed interval, combine this with !qty.isTemperature().
Prefixed and compound interval units return false; use isCompatible('degK')
for a dimensional check.
Effective Decimal settings for this quantity.
Effective conversion-cache limits, retained by derived quantities.
Effective parser settings, including an explicitly supplied parser's settings when exposed.
StaticconfigEffective settings; isolated classes keep this frozen snapshot for their lifetime.
StaticconversionImmutable limits applied separately to each instance's conversion caches.
StaticparserEffective parser settings; configuring parser options creates a separate cache scope.
A decimal scalar paired with units, with conversion, arithmetic, and comparison operations.
Import from
@neutrium/quantity. Construct quantities withnewand read the numerical result through scalar; units returns the unit expression.Remarks
Arithmetic produces quantities without changing the operands. Conversions may return the same instance or a cached result. Treat quantities and their unit arrays as immutable. Public values are getter-only; create a new quantity to change them. Counted unit records and arrays are frozen and may be shared.
Methods are shared on the prototype and require a Quantity receiver. When passing a method as a callback, use an arrow wrapper or bind it to the instance.
Example
See