getLongUnitString

wv.getLongUnitString()
getLongUnitString(unit: UnitElement[]): string

Generates a human-readable unit string from an array of unit elements.

This function takes an array of unit elements and converts them into a standardized unit string representation. Units are sorted by exponent (highest first) and formatted with appropriate symbols and exponent notations.

Parameters

unit: UnitElement[]

An array of UnitElement objects containing basicUnit, exponent, and factor properties

Returns: string

A formatted string representing the combined units with proper symbols and exponents

Examples

const units = [
  { basicUnit: BasicUnit.unitLength, exponent: 2, factor: 1 },
  { basicUnit: BasicUnit.unitMass, exponent: 1, factor: 1 },
  { basicUnit: BasicUnit.unitTime, exponent: -2, factor: 1 }
];
const result = getLongUnitString(units);
// Returns: "kg.m².s⁻²" (representing kg⋅m²⋅s⁻² for energy units)
const velocityUnits = [
  { basicUnit: BasicUnit.unitLength, exponent: 1, factor: 1 },
  { basicUnit: BasicUnit.unitTime, exponent: -1, factor: 1 }
];
const result = getLongUnitString(velocityUnits);
// Returns: "m.s⁻¹" (representing m/s for velocity)

Remarks

  • Units are automatically sorted by exponent in descending order (positive exponents first)
  • Special handling for factor values enables unit prefixes (milli-, centi-, kilo-, etc.)
  • Exponents are represented using Unicode subscript characters (², ³, ⁻¹, ⁻², ⁻³)
  • Units with exponent 1 have no subscript notation
  • Units with exponent 0 are included but display no exponent
  • Multiple units are separated by dots (.)
  • Supports all SI base units and derived units including:
  • Length (m), Mass (kg), Time (s), Electric Current (A)
  • Temperature (K), Substance Amount (mol), Luminous Intensity (cd)
  • Derived units like Force (N), Energy (J), Power (W), etc.