Today I Learned
Formatting Numbers in Python
.format()
is a good way to format numbers. A good reference is PyFormat.
For example, we can use scientific notation.
will give us
9.887200e+03
A quick and minimal version of what has been said is
{:04.2f}
means make the total length of the number 4, with 2 significance digits and using 0’s to fill up the other slots if the number is not long enough.- It’s not necessary to put in all the specifications.
d
,f
,e
can be used.- The signs can be speficied using
+
, e.g.,`{:+d}`.format(42)