Skip to content

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.

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

  • fixed x as text with exactly that many decimals, padding with zeros if needed
  • sci x as text in scientific notation, like 1.23e5 for 123000, with digits decimals
  • eng x as text in engineering notation, with the exponent a multiple of 3, like 123.457e6, with digits decimals
  • mixed_frac a number as a mixed fraction text, like “1 1/4” for 1.25, optionally rounded to the nearest 1/den
  • percent a fraction as percentage text, so 0.256 is “25.6%”, with digits decimals
  • commas x as text with commas between thousands, like “1,234,567”, with digits decimals
  • spell an integer in English words, like “forty-two”
  • short_scale a number as compact text with a short-scale word, like “2.5 million”
  • ordinal the number with its English ordinal ending: 1st, 2nd, 3rd, 4th, 11th, 21st and so on
  • human_bytes a number of bytes as text in the largest binary unit that fits, like “117.7 MiB”, with one decimal
  • dms an angle as degrees, arcminutes and arcseconds text, like 52°20′24″
  • lat a latitude in degrees as text with N or S, like 7°19′48″S
  • lon a longitude in degrees as text with E or W, like 74°0′21″W
  • roman a positive integer as a Roman numeral, or a numeral back to an integer
1
# "{x:.2f}": exactly 2 decimals
2
"{pi:.2f}""3.14"
3
# "{x:>8}": 8 wide, aligned right, < is left, ^ centered
4
"[{42:>8}]""[ 42]"
5
# "{n:08x}": hex, padded with zeros to 8, X for capitals, b binary, o octal, d decimal
6
"{255:08x}""000000ff"
7
# "{n:#06x}": # adds the 0x, 0b or 0o prefix
8
"{255:#06x}""0x00ff"
9
# "{x:,}": thousands separators, + always shows a sign, e scientific, % a percentage
10
"{1234567:,}""1,234,567"
11
# format("%5.2f", x): C's printf style
12
format("%5.2f|%-4d|", pi, 7)" 3.14|7 |"
13
# to words: in English words
14
1999 to words"one thousand nine hundred ninety-nine"
15
# to short_scale: with a scale word
16
2500000 to short_scale"2.5 million"
17
# to roman: as a Roman numeral
18
1999 to roman"MCMXCIX"
Example outputCtrl+Enter to run
fixed(x: num|quantity, digits: int) -> str

x as text with exactly that many decimals, padding with zeros if needed

Keeps units

1
pi.fixed(2)"3.14"
2
(5 km to mi).fixed(1)"3.1 mi"
Example outputCtrl+Enter to run

See also sci, commas, round

sci(x: num|quantity, digits?: int) -> str

x as text in scientific notation, like 1.23e5 for 123000, with digits decimals

1
123456.sci"1.23456e5"
2
123456.sci(2)"1.23e5"
Example outputCtrl+Enter to run

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

1
123456789 to eng"123.457e6"
2
0.00012.eng(1)"120.0e-6"
Example outputCtrl+Enter to run

See also sci, fixed

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

1
1.25 to mixed_frac"1 1/4"
2
(-7/2).mixed_frac"-3 1/2"
3
2.7 in to frac(16)"2 11/16 in"
Example outputCtrl+Enter to run

See also frac

percent(x: num|quantity, digits?: int) -> str

a fraction as percentage text, so 0.256 is “25.6%”, with digits decimals

1
0.256.percent"25.6%"
2
(1/3).percent(1)"33.3%"
Example outputCtrl+Enter to run

See also fixed

commas(x: num|quantity, digits?: int) -> str

x as text with commas between thousands, like “1,234,567”, with digits decimals

1
1234567.commas"1,234,567"
2
1234.5.commas(2)"1,234.50"
Example outputCtrl+Enter to run

See also fixed

spell(n: int) -> str

an integer in English words, like “forty-two”

Same as n to words

1
spell(42)"forty-two"
2
1999 to words"one thousand nine hundred ninety-nine"
3
-1000001 to words"minus one million one"
Example outputCtrl+Enter to run

See also ordinal, roman

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

1
2500000 to short_scale"2.5 million"
2
(-1.5e9).short_scale"-1.5 billion"
3
999999.9.short_scale"1 million"
Example outputCtrl+Enter to run

See also spell, eng

ordinal(n: int) -> str

the number with its English ordinal ending: 1st, 2nd, 3rd, 4th, 11th, 21st and so on

1
ordinal(3)"3rd"
2
(1..=4).map(ordinal)["1st", "2nd", "3rd", "4th"]
3
ordinal(112)"112th"
Example outputCtrl+Enter to run

See also spell

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

1
123456789.human_bytes"117.7 MiB"
2
1023.human_bytes"1023 B"
3
3 MB.human_bytes"2.9 MiB"
Example outputCtrl+Enter to run

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

1
52.34 to dms"52°20′24″"
2
(0.5 rad).dms"28°38′52.403″"
Example outputCtrl+Enter to run

See also lat, lon

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"
2
40.7128.lat"40°42′46.08″N"
Example outputCtrl+Enter to run

See also dms, lon

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"
2
2.35.lon"2°21′0″E"
Example outputCtrl+Enter to run

See also dms, lat

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

1
roman(2026)"MMXXVI"
2
"MCMXCIX".roman1999
Example outputCtrl+Enter to run
1
# separators and 2 decimals
2
"{1234567.891:,.2f}""1,234,567.89"
3
# hex with a prefix
4
"{255:#06x}""0x00ff"
5
# 32-bit binary
6
"{0xdeadbeef:032b}""11011110101011011011111011101111"
7
# this year
8
2026 to roman"MMXXVI"
9
# the Super Bowl after LX
10
roman("LX") + 1 to roman"LXI"
Example outputCtrl+Enter to run

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