tags:

views:

81

answers:

3

I have got output and I want to use only three values after decimal. How can i do that in Python?

+4  A: 

Use the following:

"%.3f" % x

it converts your number to a string with three decimal places.

eumiro
+3  A: 

round(number, 3)

http://docs.python.org/tutorial/floatingpoint.html

I82Much
+4  A: 

In Python 2.6 or newer you should use the str.format method:

>>> x = 15.23432
>>> '{0:.3f}'.format(x)
'15.234'
Mark Byers