[Tutor] help: funny numbers

alan.gauld@bt.com alan.gauld@bt.com
Wed, 3 Apr 2002 16:59:05 +0100


> So is there a function, method, or technique that can "clean up" the 
> noise, and approximate the value that we really want?  Or do 
> we code it ourselves?

You have to separate Pythons internal representatoon 
- which aims for maximum accuracy and the display of
that value - which is for human consumption.

In the latter case format strings are your friend.
You can format a floating point value pretty much 
anyway you like using format string codes:

>>> i = 66.6
>>> i
66.599999999999994
>>> print "%e" % i
6.660000e+001
>>> print "%f" % i
66.600000
>>> print "%3.4f" % i
66.6000
>>> print "%g" % i
66.6

There are functions to do some rounding etc too but usually 
its the display you want to affect not the stored value...

Alan g.