如何在python中将int转换为float?

时间:2022-06-24 18:20:06

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

4 个解决方案

#1


7  

Other than John's answer, you could also make one of the variable float, and the result will yield float.

除了John的答案之外,你还可以创建一个变量float,结果将产生float。

>>> 144 / 314.0
0.4585987261146497

#2


20  

To convert an integer to a float in Python you can use the following:

要在Python中将整数转换为float,您可以使用以下命令:

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这是一个浮点数,所以它是一样的。

#3


3  

You can just multiply 1.0

你可以乘以1.0

>>> 1.0*144/314
0.4585987261146497

#4


2  

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))

#1


7  

Other than John's answer, you could also make one of the variable float, and the result will yield float.

除了John的答案之外,你还可以创建一个变量float,结果将产生float。

>>> 144 / 314.0
0.4585987261146497

#2


20  

To convert an integer to a float in Python you can use the following:

要在Python中将整数转换为float,您可以使用以下命令:

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这是一个浮点数,所以它是一样的。

#3


3  

You can just multiply 1.0

你可以乘以1.0

>>> 1.0*144/314
0.4585987261146497

#4


2  

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))