如何在python中将int转换为float?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33944111/
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
How to convert int to float in python?
提问by Mr_Shoryuken
Does anyone know how to convert int
to float
.
有谁知道如何转换int
为float
.
For some reason, it keeps on printing 0. I want it to print a specific decimal.
出于某种原因,它继续打印 0。我希望它打印一个特定的小数。
sum = 144
women_onboard = 314
proportion_womenclass3_survived = sum / np.size(women_onboard)
print 'Proportion of women in class3 who survived is %s' % proportion_womenclass3_survived
采纳答案by Santosh Kumar
Other than John's answer, you could also make one of the variable float, and the result will yield float.
除了约翰的回答之外,您还可以使变量浮点之一,结果将产生浮点数。
>>> 144 / 314.0
0.4585987261146497
回答by John Ruddell
In Python 3 this is the default behavior, but if you aren't using that you can import division like so:
在 Python 3 中,这是默认行为,但如果您不使用它,您可以像这样导入除法:
>>> from __future__ import division
>>> 144/314
0.4585987261146497
Alternatively you can cast one of the variables to a float when doing your division which will do the same thing
或者,您可以在执行除法时将变量之一转换为浮点数,这将执行相同的操作
sum = 144
women_onboard = 314
proportion_womenclass3_survived = sum / float(np.size(women_onboard))
回答by Y2H
To convert an integer to a float in Python you can use the following:
要将整数转换为 Python 中的浮点数,您可以使用以下命令:
float_version = float(int_version)
The reason you are getting 0
is that Python 2 returns an integer if the mathematical operation (here a division) is between two integers. So while the division of 144 by 314 is 0.45~~~, Python converts this to integer and returns just the 0
by eliminating all numbers after the decimal point.
你得到的原因0
是如果数学运算(这里是除法)在两个整数之间,Python 2 返回一个整数。因此,虽然 144 除以 314 的结果是 0.45~~~,但 Python 将其转换为整数,并0
通过消除小数点后的所有数字来返回。
Alternatively you can convert one of the numbers in any operation to a float since an operation between a float and an integer would return a float. In your case you could write float(144)/314
or 144/float(314)
. Another, less generic code, is to say 144.0/314
. Here 144.0
is a float so it's the same thing.
或者,您可以将任何操作中的数字之一转换为浮点数,因为浮点数和整数之间的操作将返回一个浮点数。在您的情况下,您可以编写float(144)/314
或144/float(314)
. 另一个不太通用的代码是144.0/314
. 这144.0
是一个浮点数,所以它是一样的。
回答by Sourabh Dattawad
You can just multiply 1.0
你可以乘以 1.0
>>> 1.0*144/314
0.4585987261146497
回答by Jasper C.
You can literally convert it into float using:
您可以使用以下方法将其从字面上转换为浮点数:
float_value = float(integer_value)
Likewise, you can convert an integer back to float datatype with:
同样,您可以使用以下命令将整数转换回浮点数据类型:
integer_value = int(float_value)
Hope it helped. I advice you to read "Build-In Functions of Python" at this link: https://docs.python.org/2/library/functions.html
希望它有所帮助。我建议您在此链接上阅读“Python 的内置函数”:https: //docs.python.org/2/library/functions.html
回答by Prateek Gupta
The answers provided above are absolutely correct and worth to read but I just wanted to give a straight forward answer to the question.
上面提供的答案绝对正确,值得一读,但我只是想直接回答这个问题。
The question asked is just a type conversion question and here its conversion from int
data type to float
data type and for that you can do it by the function :
所问的问题只是一个类型转换问题,这里是从int
数据类型到float
数据类型的转换,为此您可以通过函数来完成:
float()
float()
And for more details you can visit thispage.
有关更多详细信息,您可以访问此页面。