gdesklets ?
Marc Boeren
m.boeren at guidance.nl
Tue May 18 09:20:15 EDT 2004
Hi,
> why I should write :
> self.__percent = int((float(self.__diff) / float(self.__max)) * 100)
>
> instead of just :
>
> self.__percent = int((self.__diff / self.__max) * 100)
look at this trick (multiply first, divide later):
>>> diff = 10
>>> max = 100
>>> diff/max * 100 // your approach without casts
0
>>> diff*100/max // trick approach without cast
10
>>> diff = 10.1
>>> diff/max * 100
10.1
>>> diff*100/max
10.1
>>>
Both aproaches work if either diff or max is a float already, except for
making the final value an integer again.
(btw, I shouldn't have used max as a variable name, but then again, it's
just an example :)
Cheerio, Marc.
More information about the Python-list
mailing list