how to make python to return floating point?
I want python to return 0.5 if I write 1/2 (a开发者_C百科nd not 1.0/2.0). how do I make python to return the floating point? (I tried using getcontext().prec form decimal module)
thanks
Ariel
Use this, or switch to Python 3.0+
from __future__ import division
from __future__ import division
Python 3.x works the way you want by default.
Python 2.2 and greater support from __future__ import division
, which makes / return floating point. There is also the // operator that still performs integer division when needed.
Also see PEP238 - Changing the Division Operator.
精彩评论