math.formatting
numbers as text: fixed decimals, commas, percentages, words, ordinals, Roman numerals and degrees-minutes-seconds
Numbers as text, laid out the way you want.
writing
Section titled “writing”Inside a string, {x} puts in the value of x. Add a colon and a format spec to control how it looks, as in Python: "{pi:.2f}" shows pi with 2 decimals. format takes the older printf style that C and many other languages use, where %5.2f means a number 5 characters wide with 2 decimals.
Tags: formatting
fixedx as text with exactly that many decimals, padding with zeros if neededscix as text in scientific notation, like 1.23e5 for 123000, with digits decimalsengx as text in engineering notation, with the exponent a multiple of 3, like 123.457e6, with digits decimalsmixed_fraca number as a mixed fraction text, like “1 1/4” for 1.25, optionally rounded to the nearest 1/denpercenta fraction as percentage text, so 0.256 is “25.6%”, with digits decimalscommasx as text with commas between thousands, like “1,234,567”, with digits decimalsspellan integer in English words, like “forty-two”short_scalea number as compact text with a short-scale word, like “2.5 million”ordinalthe number with its English ordinal ending: 1st, 2nd, 3rd, 4th, 11th, 21st and so onhuman_bytesa number of bytes as text in the largest binary unit that fits, like “117.7 MiB”, with one decimaldmsan angle as degrees, arcminutes and arcseconds text, like 52°20′24″lata latitude in degrees as text with N or S, like 7°19′48″Slona longitude in degrees as text with E or W, like 74°0′21″Wromana positive integer as a Roman numeral, or a numeral back to an integer
syntax
Section titled “syntax”1# "{x:.2f}": exactly 2 decimals2"{pi:.2f}""3.14"3# "{x:>8}": 8 wide, aligned right, < is left, ^ centered4"[{42:>8}]""[ 42]"5# "{n:08x}": hex, padded with zeros to 8, X for capitals, b binary, o octal, d decimal6"{255:08x}""000000ff"7# "{n:#06x}": # adds the 0x, 0b or 0o prefix8"{255:#06x}""0x00ff"9# "{x:,}": thousands separators, + always shows a sign, e scientific, % a percentage10"{1234567:,}""1,234,567"11# format("%5.2f", x): C's printf style12format("%5.2f|%-4d|", pi, 7)" 3.14|7 |"13# to words: in English words141999 to words"one thousand nine hundred ninety-nine"15# to short_scale: with a scale word162500000 to short_scale"2.5 million"17# to roman: as a Roman numeral181999 to roman"MCMXCIX"Functions
Section titled “Functions”fixed(x: num|quantity, digits: int) -> str
x as text with exactly that many decimals, padding with zeros if needed
Keeps units
1pi.fixed(2)"3.14"2(5 km to mi).fixed(1)"3.1 mi"sci(x: num|quantity, digits?: int) -> str
x as text in scientific notation, like 1.23e5 for 123000, with digits decimals
1123456.sci"1.23456e5"2123456.sci(2)"1.23e5"See also fixed
eng(x: num|quantity, digits?: int) -> str
x as text in engineering notation, with the exponent a multiple of 3, like 123.457e6, with digits decimals
1123456789 to eng"123.457e6"20.00012.eng(1)"120.0e-6"mixed_frac
Section titled “mixed_frac”mixed_frac(x: num|quantity, den?: int) -> str
a number as a mixed fraction text, like “1 1/4” for 1.25, optionally rounded to the nearest 1/den
A float becomes the simplest fraction that’s within 1e-12 of it. Keeps units
11.25 to mixed_frac"1 1/4"2(-7/2).mixed_frac"-3 1/2"32.7 in to frac(16)"2 11/16 in"See also frac
percent
Section titled “percent”percent(x: num|quantity, digits?: int) -> str
a fraction as percentage text, so 0.256 is “25.6%”, with digits decimals
10.256.percent"25.6%"2(1/3).percent(1)"33.3%"See also fixed
commas
Section titled “commas”commas(x: num|quantity, digits?: int) -> str
x as text with commas between thousands, like “1,234,567”, with digits decimals
11234567.commas"1,234,567"21234.5.commas(2)"1,234.50"See also fixed
spell(n: int) -> str
an integer in English words, like “forty-two”
Same as n to words
1spell(42)"forty-two"21999 to words"one thousand nine hundred ninety-nine"3-1000001 to words"minus one million one"short_scale
Section titled “short_scale”short_scale(x: num) -> str
a number as compact text with a short-scale word, like “2.5 million”
Rounds to 3 decimals, and a number under a thousand stays as it is
12500000 to short_scale"2.5 million"2(-1.5e9).short_scale"-1.5 billion"3999999.9.short_scale"1 million"ordinal
Section titled “ordinal”ordinal(n: int) -> str
the number with its English ordinal ending: 1st, 2nd, 3rd, 4th, 11th, 21st and so on
1ordinal(3)"3rd"2(1..=4).map(ordinal)["1st", "2nd", "3rd", "4th"]3ordinal(112)"112th"See also spell
human_bytes
Section titled “human_bytes”human_bytes(n: num|quantity) -> str
a number of bytes as text in the largest binary unit that fits, like “117.7 MiB”, with one decimal
Binary units step by 1024, so 1 KiB is 1024 bytes
1123456789.human_bytes"117.7 MiB"21023.human_bytes"1023 B"33 MB.human_bytes"2.9 MiB"See also fixed
dms(x: num|quantity) -> str
an angle as degrees, arcminutes and arcseconds text, like 52°20′24″
A plain number is taken as degrees
152.34 to dms"52°20′24″"2(0.5 rad).dms"28°38′52.403″"lat(x: num|quantity) -> str
a latitude in degrees as text with N or S, like 7°19′48″S
Between -90 and 90
1-7.33 to lat"7°19′48″S"240.7128.lat"40°42′46.08″N"lon(x: num|quantity) -> str
a longitude in degrees as text with E or W, like 74°0′21″W
Between -180 and 180
1-74.006 to lon"74°0′21.6″W"22.35.lon"2°21′0″E"roman(n: int) -> str / roman(s: str) -> int
a positive integer as a Roman numeral, or a numeral back to an integer
Same as n to roman
1roman(2026)"MMXXVI"2"MCMXCIX".roman1999More examples
Section titled “More examples”formatting
Section titled “formatting”1# separators and 2 decimals2"{1234567.891:,.2f}""1,234,567.89"3# hex with a prefix4"{255:#06x}""0x00ff"5# 32-bit binary6"{0xdeadbeef:032b}""11011110101011011011111011101111"7# this year82026 to roman"MMXXVI"9# the Super Bowl after LX10roman("LX") + 1 to roman"LXI"Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.