Instance Methods
Instance methods for the Unit class including string representation, prefix conversion, and generic unit conversion.
toString()
toString(): stringReturns a formatted "value unit" string. Omits unit if not provided in constructor.
playground.ts
convertByPrefix()
convertByPrefix(fromPrefix: SIPrefix, toPrefix: SIPrefix): numberParameters
| Parameter | Type | Description |
|---|---|---|
fromPrefix | SIPrefix | Source SI prefix (e.g., 'k', 'm'). |
toPrefix | SIPrefix | Target SI prefix (e.g., 'M', ''). |
Returns
number: Converted value.
playground.ts
convertFromTo()
convertFromTo(from: string, to: string): numberParameters
| Parameter | Type | Description |
|---|---|---|
from | string | Source unit with prefix (e.g., 'kg', 'cm'). |
to | string | Target unit with prefix (e.g., 'g', 'm'). |
Returns
number: Converted value. Units must be of same type (e.g., both length).
playground.ts
convert()
convert(methodName: UnitNumberMethods): numberParameters
| Parameter | Type | Description |
|---|---|---|
methodName | UnitNumberMethods | Name of static conversion method. |
Returns
number: Converted value with type-safe method selection.
playground.ts
Last updated: Sun, Jun 14, 2026 07:06:16AM (UTC)
