float, '%.2f' help... please!

Greg Ewing greg at cosc.canterbury.ac.nz
Wed Jun 28 21:35:06 EDT 2000


chibaA at TinterlogD.Tcom wrote:
> 
> I need to display a price -
> rounded up to the proper cent.  I was using this line:
> 
> print '%.2f' % (price+0.005)

Just use '%.2f' % price. The formatting operator already 
does rounding of its own:

>>> for i in xrange(0,11):
...  x = i * 0.001
...  print x, "%.2f" % x
... 
0.0 0.00
0.001 0.00
0.002 0.00
0.003 0.00
0.004 0.00
0.005 0.01
0.006 0.01
0.007 0.01
0.008 0.01
0.009 0.01
0.01 0.01

-- 
Greg Ewing, Computer Science Dept,
+--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg at cosc.canterbury.ac.nz	   +--------------------------------------+



More information about the Python-list mailing list