How can I divide two numbers in Python 2.7 and get the result with decimals?
我如何用Python 2。7除以两个数,得到小数的结果?
I don't get it why there is difference:
我不明白为什么会有差异
in Python 3:
在Python 3:
>>> 20/15
1.3333333333333333
in Python 2:
在Python中2:
>>> 20/15
1
Isn't this a modulo actually?
这不是一个模吗?
4 个解决方案
#1
75
In python 2.7, the /
operator is integer division if inputs are integers.
在python 2.7中,如果输入是整数,则/运算符是整数除法。
If you want float division (which is something I always prefer), just use this special import:
如果你想要浮动除法(这是我一直喜欢的),请使用这个特殊的导入:
from __future__ import division
See it here:
看这里:
>>> 3 / 2
1
>>> from __future__ import division
>>> 3 / 2
1.5
>>>
Integer division is achieved by using //
, and modulo by using %
整数除法是通过使用//和使用%来实现的
>>> 3 % 2
1
>>> 3 // 1
3
>>>
EDIT
编辑
As commented by user2357112
, this import has to be done before any other normal import.
正如user2357112所评论的,这个导入必须在任何其他正常导入之前完成。
#2
30
In Python 3, /
is float division
在Python 3中,/是浮点除法
In Python 2, /
is integer division (assuming int
inputs)
在Python 2中,/是整数除法(假设int输入)
In both 2 and 3, //
is integer division
在2和3中,//是整数除法。
(To get float division in Python 2 requires either of the operands be a float, either as 20.
or float(20)
)
(要在Python 2中获得浮点除法,需要其中任何一个操作数都是浮点数,或者是20。或浮动(20))
#3
13
In Python 2.x, make sure to have at least one operand of your division in float
. Multiple ways you may achieve this as the following examples:
在Python中2。x,确保在浮点数中至少有一个除法的操作数。你可以通过多种方式实现这个目标,如下所示:
20. / 15
20 / float(15)
#4
9
"/" is integer division in python 2 so it is going to round to a whole number. If you would like a decimal returned, just change the type of one of the inputs to float:
“/”是python 2中的整数除法,因此它将转到整数。如果您希望返回一个十进制数,只需将其中一个输入的类型更改为float:
float(20)/15 #1.33333333
(20)/ 15 # 1.33333333浮动
#1
75
In python 2.7, the /
operator is integer division if inputs are integers.
在python 2.7中,如果输入是整数,则/运算符是整数除法。
If you want float division (which is something I always prefer), just use this special import:
如果你想要浮动除法(这是我一直喜欢的),请使用这个特殊的导入:
from __future__ import division
See it here:
看这里:
>>> 3 / 2
1
>>> from __future__ import division
>>> 3 / 2
1.5
>>>
Integer division is achieved by using //
, and modulo by using %
整数除法是通过使用//和使用%来实现的
>>> 3 % 2
1
>>> 3 // 1
3
>>>
EDIT
编辑
As commented by user2357112
, this import has to be done before any other normal import.
正如user2357112所评论的,这个导入必须在任何其他正常导入之前完成。
#2
30
In Python 3, /
is float division
在Python 3中,/是浮点除法
In Python 2, /
is integer division (assuming int
inputs)
在Python 2中,/是整数除法(假设int输入)
In both 2 and 3, //
is integer division
在2和3中,//是整数除法。
(To get float division in Python 2 requires either of the operands be a float, either as 20.
or float(20)
)
(要在Python 2中获得浮点除法,需要其中任何一个操作数都是浮点数,或者是20。或浮动(20))
#3
13
In Python 2.x, make sure to have at least one operand of your division in float
. Multiple ways you may achieve this as the following examples:
在Python中2。x,确保在浮点数中至少有一个除法的操作数。你可以通过多种方式实现这个目标,如下所示:
20. / 15
20 / float(15)
#4
9
"/" is integer division in python 2 so it is going to round to a whole number. If you would like a decimal returned, just change the type of one of the inputs to float:
“/”是python 2中的整数除法,因此它将转到整数。如果您希望返回一个十进制数,只需将其中一个输入的类型更改为float:
float(20)/15 #1.33333333
(20)/ 15 # 1.33333333浮动