Python: getting only 1 decimal point

How to convert 45.34531 to 45.3 ?

+45
python
Aug 03 '10 at 21:41
source share
3 answers

You are trying to imagine it with only one digit:

 print "%.1f" % number 

or actually round other decimals?

 round(number,1) 

or even round strictly down?

 math.floor(number*10)/10 
+85
Aug 03 '10 at 21:42
source share
 >>> "{0:0.1f}".format(45.34531) '45.3' 

Or use the inline round:

 >>> round(45.34531, 1) 45.299999999999997 
+13
Aug 03 '10 at 21:44
source share
 round(number, 1) 
+4
Aug 03 '10 at 21:43
source share



All Articles