round() to nearest .05 ?

Batista, Facundo FBatista at uniFON.com.ar
Mon Nov 15 16:05:24 EST 2004


[tertius]

#- I'm trying to round my float total to the nearest .05 cents.
#- 
#- 12.01    should produce 12.00
#-   0.14    should produce 0.10
#-   2.28          "        2.25
#- 703.81         "        703.80
#- 
#- "%.02f"%100.0099 produces 100.01 (which I know is right)
#- No combination of round and/or "%.02f" works for me.

Don't understand. Shouldn't 0.14 produce 0.15? And 2.28, 2.30?


#- What's the best way to get there? Should I write a function 
#- to manage my 
#- rounding or is there a simpler/better way?

For this kind of math, don't use float, use Decimal:

    http://www.python.org/dev/doc/devel/lib/module-decimal.html

You'll have exact accuracy and a lot of rounding modes.

If you can not upgrade to Py2.4 yet, but have 2.3.x, install the Decimal
package from http://sf.net/projects/sigefi/

.	Facundo

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20041115/ea63bc02/attachment.html>


More information about the Python-list mailing list