By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,667 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,667 IT Pros & Developers. It's quick & easy.

Re: Odd math related issue.

P: n/a
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>

Jul 21 '08 #1
Share this Question
Share on Google+
1 Reply

P: n/a
On Jul 21, 3:52*am, Fredrik Lundh <fred...@pythonware.comwrote:
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>
The most wonderful statement in the Python language is

from __future__ import division
Jul 22 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.