Skip to main content

SI units and prefixes

Quantities that use the HasSIUnits trait can convert between any metric-prefixed form of their base unit without registering each prefixed unit individually. For example, Length registers m as its native unit, and the trait resolves km, cm, mm, μm, and so on at conversion time.

Declaring the base unit

A quantity opts in by using the trait and overriding getSIBaseUnit():

class Mass extends PhysicalQuantity
{
use HasSIUnits;

protected function getSIBaseUnit(): string
{
return 'g'; // grams, not the SI kilogram
}

// ...
}

If getSIBaseUnit() is not overridden, it returns m by default.

How resolution works

When a unit name is not found among the registered units, the trait attempts to split it into a prefix and the base unit. If the prefix is a known metric prefix and the remainder matches the base unit, the value is scaled by the corresponding power of ten.

Construction and toUnit() both use this fallback. The lookup order is: registered units first, then SI prefix resolution.

Supported prefixes

The full range from quecto (10⁻³⁰) to quetta (10³⁰) is supported.

PrefixSymbolFactorPrefixSymbolFactor
quettaQ10³⁰decid10⁻¹
ronnaR10²⁷centic10⁻²
yottaY10²⁴millim10⁻³
zettaZ10²¹microμ10⁻⁶
exaE10¹⁸nanon10⁻⁹
petaP10¹⁵picop10⁻¹²
teraT10¹²femtof10⁻¹⁵
gigaG10⁹attoa10⁻¹⁸
megaM10⁶zeptoz10⁻²¹
kilok10³yoctoy10⁻²⁴
hectoh10²rontor10⁻²⁷
decada10¹quectoq10⁻³⁰

The empty prefix (the base unit itself) maps to 10⁰.

Power notation

The trait detects whether a base unit carries a power and applies the conversion accordingly. For area and volume, the linear scale factor is raised to the power of the unit:

  • cm² to uses a factor of (10⁻²)² = 10⁻⁴.
  • mm³ to uses a factor of (10⁻³)³ = 10⁻⁹.

Both the superscript form () and the numeric form (m2) are accepted.

A conversion between two prefixed units of different powers — for instance cm² to — is rejected with an InvalidArgumentException.

Composite base units

Composite units such as m/s (Velocity) or kg/m³ (Density) can also be used as the base unit symbol. Prefix resolution still applies to the whole symbol as declared.