[Tutor] Division doesn't work
Kent Johnson
kent37 at tds.net
Thu Jan 18 14:32:23 CET 2007
Johan Geldenhuys wrote:
> Hi all,
>
> In my script I want to convert 14105 bytes to kilobytes and and this is
> what I do:
>
>> >> s = 14105
>> >> print '%0.2f' % (s/1024)
> 13.00
>
> This not correct and I don't know why. The answer is 13.77.
>
> Any pointers please that would help my in the right direction?
As well as the solutions Geofram has given, you can change the behaviour
of addition like this:
In [2]: from __future__ import division
In [3]: 14105/1024
Out[3]: 13.7744140625
Then the old-style integer division is done with //:
In [4]: 14105//1024
Out[4]: 13
More info here:
http://www.python.org/doc/2.2.3/whatsnew/node7.html
Kent
More information about the Tutor
mailing list