Float Is Ok, Int Gives Wrong Output Python 2.7
Possible Duplicate: Why doesn’t this division work in python? I have this and works fine def roi(stake, profit): your_roi = profit / stake * 100 return your_roi def f
Solution 1:
In python2.x, /
does integer division (the result is an integer, truncated downward) if both arguments are of type integer. The "easy" fix is to put:
from __future__ import division
at the very top of your script, or to construct a float out of one of the arguments before dividing:
your_roi = float(profit) / stake * 100
Python also has an integer division operator (//
), so you can still perform integer division if desired -- even if you from __future__ import division
Solution 2:
If profit is integer
, the division will be integer division instead of double division and you will have the result rounded down to the nearest integer number. Even if you format the number to float wehn printing it will still be 0 as it is rounded down on the assignment above.
Post a Comment for "Float Is Ok, Int Gives Wrong Output Python 2.7"