Problem with computing...addition

Robin Munn rmunn at pobox.com
Tue Aug 12 17:19:25 EDT 2003


Lukas Kasprowicz <nospam at maniacxs.de> wrote:
[snip]
> 
> So you see e.g. the 15th entry: "131.99000000000001"
> 
> there should be "131.99" and not this.

This is a frequently-asked question. See:

    http://www.python.org/cgi-bin/faqw.py?req=show&file=faq04.098.htp

and

    http://www.python.org/doc/current/tut/node14.html

for more details. The short version is that that's how computers handle
floating-point numbers and you should get used to it. Simply display two
digits after the decimal point when you print out the numbers. If it
really bothers you, consider doing something like:

cents = int(euros*100)

And then when you want to print:

print "%d.%d" % (cents//100, cents%100)

-- 
Robin Munn <rmunn at pobox.com> | http://www.rmunn.com/ | PGP key 0x6AFB6838
-----------------------------+-----------------------+----------------------
"Remember, when it comes to commercial TV, the program is not the product.
YOU are the product, and the advertiser is the customer." - Mark W. Schumann




More information about the Python-list mailing list