Python precision

How to write a print instruction in python that will print exactly 2 digits after a decimal number?

+6
python
source share
5 answers
x = 4.12121212 print '%.2f' % x 

Basically, the same as in C with printf.

+6
source share
 print "{0:.2f}".format(your_number) 

This is explained in detail in the Python documentation .

+11
source share
  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

+3
source share

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

0
source share

Another effective way to do this:

 import sys a = 0.5 sys.stdout.write(str('%0.2f'%a)) 
0
source share

All Articles