Odd math related issue.
fredrik at pythonware.com
Mon Jul 21 14:30:44 CEST 2008
Alexandru Palade wrote:
> However, you should be carefully because using an %i modifier for a
> what-should-be a float value truncates the value in a way you may not
> What I mean is that if you have sent 2 out of 3 bytes, the math will be
> 200/3 which with the %i modifier will print 66, rather than 66.6 (or at
> least 67 which is closer - have a look at the round() function).
My suggested workaround doesn't use floats. As for rounding, that's
more of a usability issue -- seeing the download process getting stuck
at 100% can be rather frustrating for the poor user. Better truncate
> Another thing, you could have just added a dot after the constant in
> order to promote the expression to be evaluated as float. As in
> percentage = bytes_transferred / /self/.__sessions[path].total_bytes
> * 100.
> (notice the last dot)
Did you try that?
>>> 2 / 3 * 100.
You can fix this with parentheses, but I usually recommend an explicit
"cast" instead, to make it obvious what you're doing:
result = float(a) / b
More information about the Python-list