Skip to content

units

numbers with units like km, kg or mph: calculate with them and convert between them

Permissions: network

Numbers with units, like 5 km or 60 km/h. Units carry through arithmetic, so a distance divided by a time is a speed, and to converts to another unit.

Units combine with *, / and ^, like m/s^2. Most take the metric prefixes: k for kilo, M for mega, m for milli, µ or u for micro, and so on.

Adding or comparing quantities of different kinds, like meters and seconds, is an error rather than a meaningless number.

Tags: conversion

  • simplify the quantity in the prefix of its unit that reads best, like 3.6 kW instead of 3600 W
  • si the quantity in standard metric (SI) units
  • kind the kind of quantity, like length or force, or its SI units when the kind has no name
  • all_units every unit zil knows, as a table
  • refresh_rates fetch the latest currency exchange rates now, and return how many currencies they cover
  • db a ratio of two powers in decibels, as 10 × log10(ratio), so doubling is about +3 dB
  • from_db decibels back to the ratio of powers, the reverse of db
1
# quantity: a number with a unit
2
5 km
3
# a unit power: m^2, m2 or m²
4
5 m² to cm250000 cm^2
Example outputCtrl+Enter to run
1
# + and - convert to a common unit
2
1 km + 300 m1.3 km
3
# * and / combine the units
4
100 km / 2 h50 km/h
5
# ** raises the unit too
6
(3 m) ** 29 m^2
7
# < and > compare across units
8
1 mi > 1 kmtrue
9
# to a unit
10
5 km to mi3.10686 mi
11
# in works the same as to
12
5 km in mi3.10686 mi
13
# to several units splits it across them
14
1.8 m to ft in"5 ft 10.8661 in"
15
# to a combined unit
16
100 km / 2 h to mph31.0686 mph
17
# unit sprint = 2 wk defines a unit from others, then 3 sprint to d gives 42 d
18
# unit pizza on its own makes a new unit for counting things, unrelated to the rest
19
# unit slice = pizza / 8 builds on it, then 3 slice to pizza gives 0.375 pizza
Example outputCtrl+Enter to run
1
# long, the default: best prefix, separators
2
pretty(12345678 m)"12,345.7 km"
3
# short: K, M, B and T for thousands up to trillions
4
pretty(12345678 m, "short")"12.3K km"
5
# durations and money have their own styles, shown in help("time") and help("money")
Example outputCtrl+Enter to run
kind units
length m, meter, meters, metre, metres; km, kilometer, kilometers; cm, centimeter, centimeters; mm, millimeter, millimeters; µm, um, μm, micrometer, micrometers, micron; nm, nanometer, nanometers; px, pixel, pixels; Å, angstrom, angstroms; RU, rack_unit, rack_units; point, points; pica, picas; twip, twips; tex_pt; didot, didots; cicero, ciceros; agate, agates; mi, mile, miles; yd, yard, yards; ft, foot, feet; in, inch, inches; nmi; thou, mil, mils; fathom, fathoms; chain, chains; rod, rods, perch, perches, pole, poles; span, spans; barleycorn, barleycorns; link, links; ft_us; au; ly, lightyear, lightyears; pc, parsec, parsecs; light_second, light_seconds; light_minute, light_minutes; light_hour, light_hours
mass kg, kilogram, kilograms; g, gram, grams; mg, milligram, milligrams; µg, ug, μg, microgram, micrograms; t, tonne, tonnes; ct, carat, carats; Da, dalton, daltons, amu; lb, lbs, pound, pounds; oz, ounce, ounces; st, stone; gr, grain, grains; dr, dram, drams; ozt; lbt; scruple, scruples; dwt, pennyweight, pennyweights; long_ton, long_tons; short_ton, short_tons; cwt, hundredweight, hundredweights; earth_mass, earth_masses; jupiter_mass, jupiter_masses; solar_mass, solar_masses
time s, sec, secs, second, seconds; ms, millisecond, milliseconds; µs, us, μs, microsecond, microseconds; ns, nanosecond, nanoseconds; min, mins, minute, minutes; h, hr, hrs, hour, hours; d, day, days; wk, week, weeks; mo, month, months; yr, year, years; decade, decades; century, centuries; millennium, millennia; workday, workdays; workwk, workweek, workweeks; workmo, workmonth, workmonths; workyr, workyear, workyears; schoolyr, schoolyear, schoolyears; yr_julian; yr_tropical
temperature K, kelvin; C, celsius, degC; delta_K, delta_kelvin; delta_C, delta_degC; delta_F, delta_degF; F, fahrenheit, degF; Ra, rankine
volume L, l, liter, liters, litre, litres; mL, ml, milliliter, milliliters; cc; dsp, dessertspoon, dessertspoons; cup_metric; gal_uk; qt_uk; pt_uk; gill_uk; floz_uk; gal, gallon, gallons; qt, quart, quarts; pt, pint, pints; cup, cups; floz; tbsp, tablespoon, tablespoons; tsp, teaspoon, teaspoons; gill, gills; fldr; minim, minims; butt, butts; tun, tuns; bu, bushel, bushels; pk, peck, pecks; dry_qt; dry_pt
area ha, hectare, hectares; are, ares; darcy, darcys; L_100km, lp100km; acre, acres; rood, roods; cmil; kcmil, MCM; section, sections; township, townships
speed kph, kmh; mph; kn, knot, knots
data bit, bits, shannon, shannons; nat, nats; trit, trits; dit, hartley, hartleys; octet, octets; word; B, byte, bytes; KB; MB; GB; TB; PB; KiB; MiB; GiB; TiB; PiB; kbit, Kb; Mbit, Mb; Gbit, Gb
rate bps; kbps; Mbps; Gbps
energy J, joule, joules; kJ; MJ; cal, calorie, calories; kcal, Cal; Wh; kWh; eV; cal_it; Ha, hartree, hartrees; BTU, btu; thm, therm, therms; erg, ergs; foe, foes
power W, watt, watts; kW; MW; PS; hp, horsepower; TR, ton_refrigeration; solar_luminosity
pressure Pa, pascal; kPa; MPa; bar; atm; mmHg; hPa; mbar; Torr, torr; mmH2O; psi; inHg; inH2O; ksi; barye, baryes
force N, newton, newtons; kN; gf; kgf, kilopond; lbf; ozf; pdl, poundal, poundals; dyn, dyne, dynes
angle rad, radian, radians; deg, degree, degrees; turn, turns, rev, revs, revolution, revolutions, cycle, cycles; arcmin, arcminute, arcminutes; arcsec, arcsecond, arcseconds; grad, gon, gons, gradian, gradians
current A, amp, amps, ampere, amperes; mA, milliamp, milliamps; µA, uA, μA; kA; abA, abampere, abamperes, biot, biots
voltage V, volt, volts; mV; µV, uV, μV; kV; statV, statvolt, statvolts; abV, abvolt, abvolts
resistance Ω, ohm, ohms; mΩ, mohm; kΩ, kohm; MΩ, Mohm; statohm, statohms; abohm, abohms
charge coulomb, coulombs; mAh; Ah; statC, statcoulomb, statcoulombs, franklin, franklins
capacitance farad, farads; mF; µF, uF, μF; nF; pF
conductance S, siemens; mS
magnetic_flux Wb, weber, webers; mWb; Mx, maxwell, maxwells
magnetic_field T, tesla, teslas; mT; µT, uT, μT; Gs, gauss
inductance H, henry, henries; mH; µH, uH, μH; abH, abhenry, abhenries
frequency Hz, hertz; kHz; MHz; GHz; rpm; Bq, becquerel, becquerels; Ci, curie, curies; Rd, rutherford, rutherfords
amount mol, mole, moles; mmol; µmol, umol, μmol; kmol; einstein, einsteins
concentration M, molar; mM, millimolar; µM, uM, μM, micromolar
light lm, lumen, lumens; cd, candela, candelas
illuminance lx, lux; nit, nits, nt; fc, footcandle, footcandles; fL, foot_lambert, foot_lamberts; phot, phots; sb, stilb, stilbs; lambert, lamberts
dose Sv, sievert, sieverts; mSv, millisievert, millisieverts; µSv, uSv, μSv, microsievert, microsieverts; Gy, gray, grays; rad_dose; rem
viscosity poise; cP, centipoise
kinematic_viscosity St, stokes; cSt, centistokes
flow sverdrup, sverdrups; cfm; cfs; gpm; gph
fuel_economy mpg_uk; mpg
ratio sr, steradian, steradians; ppm; ppb; ppt; permille, ‰; basis_point, basis_points, ‱; pcm; dozen, dozens; bakers_dozen; score, scores; gross; thousand; million; billion; trillion; quadrillion; quintillion; sextillion; septillion; octillion; nonillion; decillion; undecillion; duodecillion; tredecillion; quattuordecillion; quindecillion; sexdecillion; septendecillion; octodecillion; novemdecillion; vigintillion; centillion; milliard, milliards; billiard, billiards; googol; lakh, lakhs; crore, crores
simplify(q: quantity) -> num|quantity

the quantity in the prefix of its unit that reads best, like 3.6 kW instead of 3600 W

Also written to best

1
0.0012 km to best1.2 m
2
3600 J/s to best3.6 kW
3
1 kg*m^2/s^2 to best1 J
4
1.5e9 B.simplify1.5 GB
Example outputCtrl+Enter to run
si(q: quantity) -> num|quantity

the quantity in standard metric (SI) units

A named one like J (joules) or N (newtons) if there is one, otherwise base units like m, kg and s. Also written to si

1
5 mi/h to si2.2352 m/s
2
2 kWh to si7200000 J
3
72 F to si295.372 K
Example outputCtrl+Enter to run

See also simplify

kind(q: quantity) -> str

the kind of quantity, like length or force, or its SI units when the kind has no name

1
(5 N).kind"force"
2
kind(3 km / 2 h)"speed"
3
(9.8 m/s^2).kind"m/s^2"
Example outputCtrl+Enter to run

See also si, all_units

all_units() -> table

every unit zil knows, as a table

Its columns are name, full name, description, other names, kind (like length), size in SI units, and source: “units”, “informal”, “currency” or “user” for ones you defined

1
all_units().len443
2
all_units().filter(|u| u.kind == "length").map(|u| u.name).take(5)["m", "km", "cm", "mm", "µm"]
3
all_units().filter(|u| u.source == "informal")[0]{name: "banana", full: "banana", desc: "a typical banana, 17.8 cm; the internet's favorite scale", aliases: ["bananas"], kind: "length", si: 0.178 m, source: "informal"}
Example outputCtrl+Enter to run

See also simplify, refresh_rates

refresh_rates() -> int

fetch the latest currency exchange rates now, and return how many currencies they cover

Rates are otherwise kept for the number of hours in config.rates_hours, and fetched when a currency is first used

1
refresh_rates()
Example outputCtrl+Enter to run

See also all_units

db(ratio: num, kind: str = “power”) -> float

a ratio of two powers in decibels, as 10 × log10(ratio), so doubling is about +3 dB

For a ratio of voltages or sound pressures, pass “amplitude”, which uses 20 × log10

1
db(2)3.0103
2
db(1000)30
3
db(2, "amplitude")6.0206
Example outputCtrl+Enter to run

See also from_db, log

from_db(db: num, kind: str = “power”) -> float

decibels back to the ratio of powers, the reverse of db

With “amplitude”, the ratio of voltages or sound pressures

1
from_db(3)1.99526
2
from_db(-6, "amplitude")0.501187
Example outputCtrl+Enter to run

See also db

1
# speed from distance and time
2
100 km / 2 h to mph31.0686 mph
3
# feet and inches
4
1.8 m to ft in"5 ft 10.8661 in"
5
# feet and inches written together
6
5 ft 11 in to cm180.34 cm
7
# 300 miles at 65 mph
8
300 mi / 65 mph to h min"4 h 36.9231 min"
9
# downloading 4 GB at 100 Mbps
10
4 GB / 100 Mbps to min5.33333 min
11
# why a 1 TB disk shows less: GiB are bigger than GB
12
1 TB to GiB931.323 GiB
13
# area
14
3 m * 4 m to ft^2129.167 ft^2
15
# a 10 km run in an hour, as minutes per mile
16
1 h / 10 km to min/mi9.65606 min/mi
17
# the force of 70 kg under gravity
18
9.81 m/s^2 * 70 kg to N686.7 N
19
# a month of a 60 W bulb
20
60 W * 8 h * 30 to kWh14.4 kWh
21
# a fever in Celsius
22
101 F to C38.3333 C
23
# a 1080p screen at 96 dpi
24
1080 px to in11.25 in
25
# twice the power, in decibels
26
db(2)3.0103
27
# how much more power +3 dB is
28
from_db(3)1.99526
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.