How to convert int to float in python?

Does anyone know how to convert intto float.

For some reason, it keeps typing 0. I want it to print a specific decimal number.

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
source share
4 answers

Besides John's answer, you can also make one of the float variables, and the result will give a float.

>>> 144 / 314.0
0.4585987261146497
+5
source

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

float_version = float(int_version)

, 0, , Python 2 , ( ) . , 144 314 0,45 ~~~, Python 0 .

float, float integer float. float(144)/314 144/float(314). , - 144.0/314. 144.0 - float, .

+12

1.0

>>> 1.0*144/314
0.4585987261146497
+1

Python 3 , , :

>>> from __future__ import division
>>> 144/314
0.4585987261146497

Alternatively, you can apply one of the variables to the float when doing the division, which will do the same

sum = 144
women_onboard = 314
proportion_womenclass3_survived = sum / float(np.size(women_onboard))
0
source

Source: https://habr.com/ru/post/1617647/


All Articles