snippetpythonCritical
How can I force division to be floating point? Division keeps rounding down to 0?
Viewed 0 times
roundinghowfloatingcanforcekeepsdivisiondownpoint
Problem
I have two integer values
a and b, but I need their ratio in floating point. I know that `a In 3.x, the behaviour is reversed; see Why does integer division yield a float instead of another integer? for the opposite, 3.x-specific problem.Solution
In Python 2, division of two ints produces an int. In Python 3, it produces a float. We can get the new behaviour by importing from
__future__.>>> from __future__ import division
>>> a = 4
>>> b = 6
>>> c = a / b
>>> c
0.66666666666666663Code Snippets
>>> from __future__ import division
>>> a = 4
>>> b = 6
>>> c = a / b
>>> c
0.66666666666666663Context
Stack Overflow Q#1267869, score: 860
Revisions (0)
No revisions yet.