Main Content

Integer v float - Python

Archive - Originally posted on "The Horse's Mouth" - 2007-11-12 16:26:03 - Graham Ellis

If you had f failures in s samples, what proportion failed? .6666666666 you might correctly say - but Python might not come up with that answer if you have two integer counts

>>> s = 6
>>> f = 4


And that gives:

>>> f/s
0


Will that be better if we turn it into percentages?

>>> f*100/s
66


Yes - somewhat better but still inaccurate - the real solution is to convert one of the operands into a float:


>>> f*100/float(s)
66.666666666666671