Skip to main content

Units Reference

Every unit Stof supports, for looking things up. Types & Units covers how units work. Each unit's abbreviation works after a number (5ft, 20kg), and either the abbreviation or the full name works as a type:

equivalence.stofPlayground ↗
#[main]
fn main() {
  const a: km = 3km;
  const b: kilometers = 3km;
  pln(a == b);

  pln((135cm + 5ft + 123in as m).round(4));
}
Output

When two different units of the same kind combine, the result is in the larger of the two (500m + 1km is in km). Mixed angles always give radians.

Length​

UnitFull Name
kmkilometers
hmhectometers
dcmdecameters
mmeters
dmdecimeters
cmcentimeters
mmmillimeters
ummicrometers
nmnanometers
mimiles
ydyards
ftfeet
ininches

Mass​

UnitFull Name
Gtgigatonnes
Mtmegatonnes
ttonnes
kgkilograms
ggrams
mgmilligrams
ugmicrograms
ngnanograms
pgpicograms
Tontons
lblbs (imperial pounds)
ozounces

Time​

UnitFull Name
daydays
hrhours
minminutes
sseconds
msmilliseconds
usmicroseconds
nsnanoseconds

Time::now() returns milliseconds, so adding or subtracting any other time unit works directly. The result is in the larger unit (Time::now() + 1hr is in hours), so before comparing it with a plain number of milliseconds (Ex. a time from JSON), give the number its units: const at: ms = record.expires;.

Temperature​

UnitFull Name
Kkelvin
Ccelsius
Ffahrenheit

Angles​

UnitFull NameRange
radradiansclamped to ±360° equivalent
degdegreesclamped to ±360°
pradpradians (positive radians)clamped to [0°, 360°)
pdegpdegrees (positive degrees)clamped to [0°, 360°)

The positive versions keep angles between 0 and a full turn: casting -90deg to pdeg gives 270pdeg.

Memory (Decimal)​

UnitFull Name
bit / bits
byte / bytes
KBkilobytes
MBmegabytes
GBgigabytes
TBterabytes
PBpetabytes
EBexabytes
ZBzettabytes
YByottabytes

Memory (Binary)​

UnitFull Name
KiBkibibytes
MiBmebibytes
GiBgibibytes
TiBtebibytes
PiBpebibytes
EiBexbibytes
ZiBzebibytes
YiByobibytes

These are two different sets of units: MB is 1,000,000 bytes and MiB is 1,048,576, and the gap grows with size. When something says GB but means GiB, as is common in computing, cast to the unit it actually means.