On Tue, 2010-08-24 at 16:04 +0530, Arulalan T wrote:
> 
> I am getting the following output in python while adding two float nos.
> 
> >>>a=79.73
> >>>b=0.5
> >>> a+b
> 80.230000000000004
> 
> I need exactly 2 precision point in this float value. i.e. 80.23

That's a limitation of binary floating point. The number 80.23 cannot be
represented exactly. Use the decimal module if you want exact numbers:
<http://docs.python.org/library/decimal.html>. For more information look
at the Python Docs on floating point:
<http://docs.python.org/tutorial/floatingpoint.html>.

As you'll see there, Python 3.1 will print the result in the way you
want, but if you want more accurate arithmetic, multiply all your
numbers by 100 and use integers instead or use the decimal module.

-- 
Roshan George

_______________________________________________
ILUGC Mailing List:
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc

Reply via email to