[Tutor] Python 2.5.4 - error in rounding

wesley chun wescpy at gmail.com
Fri May 21 23:16:20 CEST 2010


correct, it is a floating point issue regardless of language.. it's
not just Python. you cannot accurately represent repeating fractions
with binary digits (bits). more info specific to Python here:
http://docs.python.org/tutorial/floatingpoint.html

also, keep in mind that '%f' is not a rounding operation... it just
converts floats to strings. if you want to round, you need to use both
string formatting as well as the round() built-in function.

finally, +1 on using decimal.Decimal as necessary comfortwise.

cheers,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list