将 int 转换为双 Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31519987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:08:13  来源:igfitidea点击:

Convert int to double Python

python

提问by The Nightman

I can't seem to find the answer to this on this site, though it seems like it would be common enough. I am trying to output a double for the ratio of number of lines in two files.

我似乎无法在这个网站上找到这个问题的答案,尽管这似乎很常见。我正在尝试为两个文件中的行数之比输出一个双精度值。

#Number of lines in each file
inputLines = sum(1 for line in open(current_file))
outputLines = sum(1 for line in open(output_file))

Then get the ratio:

然后得到比例:

ratio = inputLines/outputLines

But the ratio always seems to be an int and rounds even if I initialize it like:

但即使我将其初始化为:

ratio = 1.0

Thanks.

谢谢。

采纳答案by goncalopp

In python 2, the result of a division of two integers is always a integer. To force a float division, you need to use at least one float in the division:

在 python 2 中,两个整数相除的结果总是一个整数。要强制进行浮点除法,您需要在除法中至少使用一个浮点数

ratio = float(inputLines)/outputLines

ratio = float(inputLines)/outputLines

Be careful not to do ratio = float(inputLines/outputLines): although that results in a float, it's one obtained afterdoing the integer division, so the result will be "wrong" (as in "not what you expect")

注意不要这样做ratio = float(inputLines/outputLines):虽然这会导致浮点数,但它是在进行整数除法获得的,因此结果将是“错误的”(如“不是您期望的”)

In python 3, the integer division behavior was changed, and a division of two integers results in a float. You can also use this functionality in python 2.7, by putting from __future__ import divisionin the begging of your files.

在 python 3 中整数除法行为发生了变化,两个整数的除法导致浮点数。你也可以在 python 2.7 中使用这个功能,通过放入from __future__ import division你的文件中。



The reason ratio = 1.0does not work is that types (in python) are a property of values, not variables- in other words, variables don't have types.

原因ratio = 1.0不起作用是类型(在python中)是values的属性,而不是变量- 换句话说,变量没有类型。

a= 1.0 # "a" is a float
a= 1   # "a" is now a integer

回答by Mihai Maruseac

You need to cast one of the terms to a floating point value. Either explicitly by using float(that is ratio = float(a)/bor ratio=a/float(b)) or implicitly by adding 0.0: ratio = (a+0.0)/b.

您需要将其中一项转换为浮点值。显式地使用float(即ratio = float(a)/bratio=a/float(b))或隐式地添加0.0: ratio = (a+0.0)/b

If using Python 2 you can from __future__ import divisionto make division not be the integral one but the float one.

如果使用 Python 2,您可以from __future__ import division使除法不是整数而是浮点数。

回答by Underyx

Indeed, Python 2 returns an integer when you divide an integer. You can override this behaviour to work like in Python 3, where int / int = floatby placing this at the top of your file:

事实上,当你除以一个整数时,Python 2 返回一个整数。您可以覆盖此行为以像在 Python 3 中一样工作,int / int = float通过将其放置在文件顶部:

from __future__ import division

Or better yet, just stop using Python 2 :)

或者更好的是,停止使用 Python 2 :)

回答by adrianus

Are you using Python 2.x?

你在使用 Python 2.x 吗?

Try using

尝试使用

>>> from __future__ import division
>>> ratio = 2/5
0.4

to get a floatfrom a division.

float从一个部门得到一个。

Initializing with ratio = 1.0doesn't work because with ratio = inputLines/outputLinesyou are re-assigning the variable to int, no matter what it was before.

初始化ratio = 1.0不起作用,因为ratio = inputLines/outputLines你正在将变量重新分配给 int,无论它之前是什么。