Sunday 8 December 2019

math - python simple percentage calculation returning 0




Okay this seems to be really trivial but I'm not able to figure this out.
I need to do a basic percentage calculation.
This is my code:




corr = 205
score = 100 * (corr / 225 )
print score


But the result is 0.
How come? There aren't a whole of different ways to write this, but I did shuffle this around and tried adding int()s and float()s, but the results was always 0.


Answer



Adding on to @Germano's answer,




Python2.7



>>> corr = 205
>>> score = 100 * (corr / 225)
>>> print score
0


Python3 fixes the automatic casting.




>>> corr = 205
>>> score = 100 * (corr / 225)
>>> print(score)
91.11111111111111


See How can I force division to be floating point? Division keeps rounding down to 0


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...