Formatting numbers#
Every number handcalcs shows goes through a Python
format code,
the part after the colon in f"{value:.2f}". The default is .5g: up to five
significant figures, switching to scientific notation for very large or small values.
An integer keeps all its digits under a g format code (E = 200000, not 2e+05);
a code such as .2f or .3e applies to integers too. The elements of a list, tuple or
numpy array each take the format code, and a two-dimensional numpy array is shown as a
matrix.
a = 1234.56789
b = 0.000012345
c = a * b
a = 1234.6
b = 1.2345e-05
c = a * b = 1234.6 * 1.2345e-05 = 0.015241
Common format codes#
Code |
Meaning |
|
|---|---|---|
|
5 significant figures (default) |
|
|
3 significant figures |
|
|
2 decimal places |
|
|
no decimals |
|
|
scientific, 3 decimals |
|
|
thousands separator, 1 decimal |
|
The LaTeX and Markdown renderers typeset scientific notation as
1.235 × 103 rather than 1.235e+03. The same value with four codes:
x = 1234.56789 * 1 # hc: -f .5g
x_3g = 1234.56789 * 1 # hc: -f .3g
x_2f = 1234.56789 * 1 # hc: -f .2f
x_3e = 1234.56789 * 1 # hc: -f .3e
$$\begin{aligned} x &= 1234.56789 \cdot 1 = 1234.6 \\ x_{\mathrm{3g}} &= 1234.56789 \cdot 1 = 1.23 \times 10^{3} \\ x_{\mathrm{2f}} &= 1234.56789 \cdot 1 = 1234.57 \\ x_{\mathrm{3e}} &= 1234.56789 \cdot 1 = 1.235 \times 10^{3} \end{aligned}$$
Numbers you type inside a formula keep their digits as written (1234.56789 above);
the format code applies to calculated values and to the values of parameters.
Where to set it#
From the narrowest scope to the widest:
One line: a command at the end of the line.
a = 2.0
b = 1 / 3
c = a * b # hc: -f .2f
d = a * b
$$\begin{aligned} a &= 2 \\ b &= \frac{1}{3} = 0.33333 \\ c &= a \cdot b = 2.00 \cdot 0.33 = 0.67 \\ d &= a \cdot b = 2 \cdot 0.33333 = 0.66667 \end{aligned}$$
The rest of the calculation: a command on its own line.
a = 2.0
b = 1 / 3
# hc: -f .3e
c = a * b
d = c / 7
$$\begin{aligned} a &= 2 \\ b &= \frac{1}{3} = 0.33333 \end{aligned}$$
$$\begin{aligned} c &= a \cdot b = 2.000 \times 10^{0} \cdot 3.333 \times 10^{-1} = 6.667 \times 10^{-1} \\ d &= \frac{c}{7} = \frac{6.667 \times 10^{-1}}{7} = 9.524 \times 10^{-2} \end{aligned}$$
Every calculation in the session: the format_code option.
import handcalcs
handcalcs.set_option("format_code", ".3g")
Every session, permanently: set the option, then save it.
handcalcs.set_option("format_code", ".3g")
handcalcs.save_config()
In Jupyter there is also a line magic for the session setting:
%hc_format_code .3g
See Configuration for how saved options are stored and reset.
Values that are not plain numbers#
TrueandFalseare shown as words, not as1and0.Dates and times are shown in their usual
str()form.Collections (
list,tuple,set,dict) format each element; long ones are shortened with an ellipsis.
ok = 3 > 2
today = datetime.date(2026, 10, 20)
xs = [1 / 3, 2 / 3, 1.0] # hc: -f .2f
long_list = list(range(20))
$$\begin{aligned} \mathrm{ok} &= 3 \gt 2 = \mathrm{True} \\ \mathrm{today} &= \mathrm{datetime}.\mathrm{date}\left(2026, 10, 20\right) = 2026-10-20 \\ \mathrm{xs} &= [0.33, 0.67, 1.00] \\ \mathrm{long}_{\mathrm{list}} &= \operatorname{list}\left(\operatorname{range}\left(20\right)\right) = [0, 1, 2, \ldots, 17, 18, 19] \end{aligned}$$
Any other object is formatted with the format code if it supports one (as forallpeople quantities do); otherwise its
str()is shown. An object whosestr()looks like<MyObject at 0x...>will be shown that way, so give your own classes a readable__str__or__format__.
Next: Configuration.