Precision?

Ron Adam rrr at ronadam.com
Sun May 15 15:25:43 EDT 2005


tiissa wrote:
> Steffen Glückselig wrote:
> 
>>>>>1.0 + 3.0 + 4.6
>>
>>8.5999999999999996
>>
>>Ehm, how could I get the intuitively 'correct' result of - say - 8.6?
>>;-)
> 
> 
> You may find annex B of the python tutorial an interesting read:
> http://docs.python.org/tut/node16.html

In addition to what you find in the above link, the round function can 
be used.

p = 1	#digits of precision after decimal
a, b, c = 1.0, 3.05, 4.6
print round(a+b+c,p)

-> 8.6

You also have the option to use the print statements '%' operator to 
format the output.


a, b, c = 1.0, 3.05, 4.6
print "%.1f"%(a+b+c)

-> 8.6


_Ron



More information about the Python-list mailing list