Version: dev

parse_unit

Stable function · Units

Tier: stable · Kind: function · Topic: Units

JuliaTypeScriptPythonRustGo
not exportedparseUnitnot exportedparse_unitParseUnit

Julia

Julia does not export this function.

TypeScript

function parseUnit(unitStr: string): ParsedUnit

Parse a unit string into canonical SI dimensions plus scale factor.

STRICT: an unparseable unit string is an ERROR. It is never silently collapsed to dimensionless — a dimensionless fallback is a claim about the quantity, and a wrong one, which both hides real mismatches (everything compares equal to everything) and manufactures false ones (against genuinely dimensional operands).

Callers that must degrade gracefully rather than throw — the validators, which want to leave a dimension UNKNOWN and carry on — use tryParseUnit , which returns null instead.

Throws

  • UnitConversionError — on an unknown unit name, a malformed token, unbalanced parentheses, or a misused offset unit.

Source

Python

Python does not export this function.

Rust

pub fn parse_unit(unit_str: &str) -> Result<Unit, UnitError>

Parse a unit string into a Unit struct

Supports common scientific unit notations:

  • base units ("m", "s", "kg", "mol", "K", "Pa", …)
  • products ("kg*m", "N*m")
  • quotients ("m/s", "mol/L")
  • parenthesised groups ("J/(mol*K)")
  • integer powers ("m^2", "s^-1")
  • dimensionless ("", "1", "dimensionless")

Source · Rust docs

Example (convert_units)

let km = parse_unit("km")?;
let m = parse_unit("m")?;
assert_eq!(convert_units(2.5, &km, &m)?, 2500.0);

// Incompatible dimensions are an error.
assert!(convert_units(1.0, &km, &parse_unit("s")?).is_err());

From docs/api/examples/rust/units.rs

Go

func ParseUnit(s string) (Unit, error)

ParseUnit parses a unit string into a Unit. Grammar:

unit     := term ( ('*'|'/')? term )*
term     := atom ( ('^'|'**') exponent )?
exponent := integer | decimal | '(' integer '/' integer ')'
atom     := '1' | symbol | '%' | '(' unit ')'

EXPONENTS ARE RATIONAL. “1/s^0.5” (SDE noise intensity), “s^(-1/2)” and “m^(3/2)” are legitimate corpus units; an integer-only exponent grammar cannot express them and — under the hard-error severity for an unparseable unit — falsely rejects the files that carry them.

‘*’ AND ‘/’ ARE ONE PRECEDENCE LEVEL, applied strictly left to right: “J/mol*K” is (J/mol)*K, NOT J/(mol*K), and “a/b/c” is a/(b*c). Giving ‘*’ a tighter binding silently negates the exponent of every factor after a ‘/’.

Whitespace separates tokens and, between two terms, means MULTIPLICATION — the SI style “kg m^2 s^-2” and the corpus’s “ppb^-1 s^-1” (== ppb^-1 * s^-1). An explicit ‘*’ is equivalent. ‘**’ is accepted as a synonym for ‘^’ (the Python/pint spelling, e.g. “Pa*m**3”).

A unit string carries DIMENSIONS ONLY: there is no species-tag convention, so the “C” of “kg C/m^2” is the coulomb, not a carbon tag.

The empty string, “1” and “dimensionless” are the dimensionless unit. Non-ASCII spellings (μg, °C, W/m², J/(kg·K), Ω) are normalized first — see normalizeUnitString. Examples that must parse:

"m", "m/s", "m/s^2", "kg*m^2/s^3", "cm^3/molec/s", "mol/mol", "1/s",
"Pa", "J/(mol*K)", "Pa*m**3", "ppb^-1 s^-1", "μg/m^3", "°C", "1/s^0.5",
"s^(-1/2)", "W/m²", "J/(kg·K)", "%", "".

Source · Go docs