[Tutor] Question on rounding
Brad Chandler
mbc2@netdoor.com
Tue, 9 Jan 2001 17:06:12 -0600
I know this must have been discussed before, but I'm having a problem with
rounding. My hand calculator gives me the following: 25350 * .0055 =
139.425
But Python gives me 139.42499999999998. Now what I want is to round that up
to 139.43, so I've been using round(x,2). But my numbers have been slightly
off in python and I've just now discovered why. I think I can just use
round(x,3) and then round(x,2) to get the desired result, but that just
seems messy and I'm wondering if there is a better, more correct way of
doing it. Below is an example of my interpreter session.
>>> x=25350.00*.0055
>>> x
139.42499999999998
>>> round(x,2)
139.41999999999999
>>> y = round(x,3)
>>> y
139.42500000000001
>>> round(y,2)
139.43000000000001