Formatting Numbers in Python
Formatting numbers in python using format
.format()
is a good way to format numbers. A good reference is PyFormat.
For example, we can use scientific notation.
print "{:e}".format(9887.2)
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)
Planted:
by Lei Ma;
References:
Similar Articles:
L Ma (2016). 'Formatting Numbers in Python', Datumorphism, 10 April. Available at: https://datumorphism.leima.is/til/programming/formating-numbers-python/.