Working with decimal points

Fredrik Lundh fredrik at pythonware.com
Sat Apr 8 11:35:21 EDT 2006


"Byte" wrote:

> How come:
>
> sum = 1/4
> print sum
>
> returns 0?

because 1 and 4 are integer objects, so 1/4 is an integer division, which
rounds down to the nearest integer.

> 1/4=0.25, not 0. How do I fix this?

use floating point numbers:

    1.0/4.0 = 0.25

or convert one of the numbers to a float:

    float(1)/4 = 0.25

</F>






More information about the Python-list mailing list