在Python中使用除法运算符时,如何获取十进制值? [英] How do I get a decimal value when using the division operator in Python?
本文介绍了在Python中使用除法运算符时,如何获取十进制值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,标准除法符号'/'舍入为零:
For example, the standard division symbol '/' rounds to zero:
>>> 4 / 100
0
但是,我希望它返回0.04.我该怎么用?
However, I want it to return 0.04. What do I use?
推荐答案
共有三个选项:
>>> 4 / float(100)
0.04
>>> 4 / 100.0
0.04
与C,C ++,Java等或
which is the same behavior as the C, C++, Java etc, or
>>> from __future__ import division
>>> 4 / 100
0.04
您还可以通过将参数-Qnew
传递给Python解释器来激活此行为:
You can also activate this behavior by passing the argument -Qnew
to the Python interpreter:
$ python -Qnew
>>> 4 / 100
0.04
第二个选项将是Python 3.0中的默认选项.如果要使用旧的整数除法,则必须使用//
运算符.
The second option will be the default in Python 3.0. If you want to have the old integer division, you have to use the //
operator.
编辑:感谢ΤΖΩΤζΙΟΥ!
这篇关于在Python中使用除法运算符时,如何获取十进制值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文