Skip to content

This site describes solve-engine as it is on main: 2.43.0, which npm does not have yet. npm installs 2.40.0, so a page may show an answer that version does not give yet.

convertUnit

function convertUnit(
value,
from,
to
): number;

Defined in: packages/engine/src/uom/UomConverter.ts:201

Convert value from unit from to unit to.

A conversion between scales with different zeros (the temperatures) that lands within rounding of zero is zero, so the value and not only its display is the zero the two scales share; see OFFSET_ZERO_TOLERANCE.

Does not validate that the conversion is possible, callers should check with canConvert first if from/to aren’t already known-good.

THROWS a RangeError for an unknown unit or a cross-measure pair. VM.ts’s extractDurationMs() depends on that, see convertRaw.

This used to memoize a per-pair conversion factor in an LFU cache. The cache is gone: computing the factor is now two Map reads, while the cache cost a template-string key allocation on every call plus the LFU’s own frequency bookkeeping, so it was slower than the work it avoided. Removing it is bit-exact, not merely close: the cache stored convertRaw(1, f, t), which is exactly fromRatio / toRatio, and then multiplied, which is the identical expression convertRaw(value, f, t) evaluates. It also deleted the special case that existed only to keep offset-based temperature pairs out of a factor cache.

ParameterType
valuenumber
fromstring
tostring

number