Odd math related issue.

Fredrik Lundh fredrik at pythonware.com
Mon Jul 21 04:52:33 EDT 2008


Robert Rawlins wrote:

> I’ve got what seems to me to be a totally illogical math issue here 
> which I can’t figure out. Take a look at the following code:
> 
>         /self/.__logger.info(/"%i / %i"/ % (bytes_transferred, 
> /self/.__sessions[path].total_bytes))
> 
>         percentage = bytes_transferred / 
> /self/.__sessions[path].total_bytes * 100
> 
>         /self/.__logger.info(/"%i"/ % percentage)
> 
> Seems fairly straight forward, you would think. It takes two values and 
> calculates the percentage of one from the other, however, percentage 
> always comes back as ‘0’ for some reason, look at this log output.

if you divide two integers, you'll get an integer back (in Python 2.X, 
at least).  quick fix:

      percentage = bytes_transferred * 100 / total_bytes

</F>




More information about the Python-list mailing list