How to write a print instruction in python that will print exactly 2 digits after a decimal number?
x = 4.12121212 print '%.2f' % x
Basically, the same as in C with printf.
print "{0:.2f}".format(your_number)
This is explained in detail in the Python documentation .
f = 4.55556 print "{0:.2f}".format(f)
There is also a special module for decimal places with a fixed point. More details: http://docs.python.org/library/decimal.html
this is a template tag of the django float format, which may be interesting for you to read ...
on line 92:
django float format template tag
Another effective way to do this:
import sys a = 0.5 sys.stdout.write(str('%0.2f'%a))