16

Does anyone know how to convert `int`

to `float`

.

For some reason, it keeps on printing 0. I want it to print a specific decimal.

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

10

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

27

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

```
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.

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.

3

In Python 3 this is the default behavior, but if you aren't using that you can import division like so:

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

Thank you sooooooo much it worked !!! : - Mr_Shoryuken 2015-11-26 17:24

@Mr_Shoryuken yep! glad I could help. When you are able to if you can mark the answer as accepted so that this question can be resolved I'd appreciate it! : - John Ruddell 2015-11-26 17:24

3

You can just multiply `1.0`

```
>>> 1.0*144/314
0.4585987261146497
```

Thank you it work - Mr_Shoryuken 2015-11-28 18:09

`.0`

to the number). Which is why I like to make the default behavior do what I want it to do :) but regardless were all on here for rep so +1 from me : - John Ruddell 2015-11-26 17:27