[FRPythoneers] I must be sitting too close to the monitor.

Greg Knaddison Greg.Knaddison at azuresolutions.com
Thu Jan 13 11:00:08 MST 2005


> -----Original Message-----
> From: frpythoneers-bounces at lists.community.tummy.com 
> [mailto:frpythoneers-bounces at lists.community.tummy.com] On 
> Behalf Of Ben Harding
> Sent: Thursday, January 13, 2005 9:45 AM
> To: Front Range Python Users -- General Discussion
> Subject: Re: [FRPythoneers] I must be sitting too close to 
> the monitor.
> I bet there are others on this list that really know about 
> this, and I would be interested in learning more about the 
> floating point representation in general and that used by Python.

I have run into this problem before trying to deal with monetary

The solutions were either

1) Immediately multiply the number by 100 so that the cents are moved
into the first unit position.  Then you get integers and can deal with
them fine and you divide them by 100 anytime you want to output them.
>From your example:

change = 0.05
change *= 100
while change >= 1:
    print change/100
    change = change - 1

2) Since the numbers are very very close, just use a round anytime you
compare it to something exact.  So, from your example:

change = 0.05
while round(change, 2) >= 0.01:
    print round(change,2)
    change = change - 0.01

By the way, I use Jython2.1 and not Python - so if my examples behave a
little differently in Python I'd be interested to hear another solution.


More information about the FRPythoneers mailing list