简体   繁体   English

如何让python返回浮点?

[英]how to make python to return floating point?

I want python to return 0.5 if I write 1/2 (and not 1.0/2.0). 如果我写1/2(而不是1.0 / 2.0),我希望python返回0.5。 how do I make python to return the floating point? 如何让python返回浮点? (I tried using getcontext().prec form decimal module) (我尝试使用getcontext()。prec form decimal module)

thanks 谢谢

Ariel 阿里尔

使用它,或切换到Python 3.0+

from __future__ import division
from __future__ import division

Python 3.x works the way you want by default. 默认情况下,Python 3.x以您希望的方式工作。

Python 2.2 and greater support from __future__ import division , which makes / return floating point. Python 2.2和更大的支持from __future__ import division ,它使得/返回浮点。 There is also the // operator that still performs integer division when needed. 还有//运算符在需要时仍然执行整数除法。

Also see PEP238 - Changing the Division Operator . 另请参阅PEP238 - 更改分区操作员

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM