Rounding curiosity

Grant Edwards grante at
Wed Nov 17 05:24:22 CET 2004

On 2004-11-17, PyBo <bartley9 at> wrote:

> What's wrong with this picture?

Wrong is in the mind of the beholder.  ;)

>>>> x = 500000000.0
>>>> y = x / (1024 * 1024)
>>>> y
> 476.837158203125
>>>> z = round(y, 2)
>>>> z
> 476.83999999999997
> I have tried this and get the same result using different
> CPU's and operating systems.

There's a reason for that:

> Obviously, '476.83999999999997' is not rounded to two decimal
> places.

Of course it isn't.  Computers don't use decimal.  They use
binary: aye, there's the rub...

> Or am I doing something wrong?

You're using binary floating point math without understanding
it.  If you tell us what you're actually trying to accomplish,
we can probably tell you how to do it.  

If you're just worried about how it looks on the screen:

>>> print "%0.2f" % round(5e8/(1024*1024),2)

If you want the computer to fib for you, it will. :)

Grant Edwards                   grante             Yow!  PUNK ROCK!! DISCO
                                  at               DUCK!! BIRTH CONTROL!!

More information about the Python-list mailing list