44

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 

See also: How can I force division to be floating point? Division keeps rounding down to 0? for the underlying problem. Doing the conversion is the natural way to avoid this problem, but there are also other reasons why such conversion might be necessary.

1
  • sum = sum + 0.0 or simply sum += 0.0 it's enough to a integer number getting to a float. I'm just learning python but it seems simple (docs.python.org/3/library/… says: "Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the “narrower” type is widened to that of the other, where integer is narrower than floating point, which is narrower than complex. A comparison between numbers of different types behaves as though the exact values of those numbers were...") Commented Nov 27, 2021 at 17:43

6 Answers 6

58

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.

Sign up to request clarification or add additional context in comments.

Comments

18

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 

1 Comment

I prefer to not do this only because it makes you have to change input that could come from a database or somewhere else (by adding .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 :)
6

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

Comments

5

You can just multiply 1.0

>>> 1.0*144/314 0.4585987261146497 

Comments

5

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

Comments

1

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 :

float()

And for more details you can visit this page.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.