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.

Answer from Y2H on Stack Overflow
Discussions

Converting large int value to float and back to int messes up the value?
There's a long-winded and very technical explanation about how floats and ints are implemented and why this happens for those numbers, but the reason why it must happen for some numbers is pretty simple: Python ints are unbounded, which means there are infinitely many ints you can have in Python. Python floats are 64-bit numbers, which means there are only 2^64 of them. Since there aren't infinitely many floats, you can't make int -> float -> int work for all numbers. The technical explanation is that ints are stored as a list of digits (essentially), so it can be as long as you want, but floats are basically a fraction, stored as a triple of numbers (s,p,q) each of a fixed-size and the numeric value of the float is (-1)^s*q*2^p. If this doesn't make sense, read up on "double-precision floating point" https://en.wikipedia.org/wiki/Double-precision_floating-point_format . The consequence of this is that floats can store very small and very large numbers, but there's a lot of gaps in between, and if your int falls in one of those gaps then when you turn it into a float with float() it picks the "nearest" one instead, so when you turn it back to an int you get the wrong value. More on reddit.com
🌐 r/learnpython
12
9
February 22, 2024
Float and int function
float() and int() are mainly use to convert strings to numbers. They actually create a number from the strings. int() is also often use to create an integer number from a float number. A classic example is when you ask the user for a number. The input function always returns a string of character. To perform calculation you need a number (int or float). So you must create a number from that string. As in: string_number = input("Enter temperature in °F (integer): ") temp_far = int(string_number) temp_cel = (temp_far-32) * (5/9) # the result here is a float print("Temperature in celsius (float)", temp_cel) temp_cel_int = int(temp_cel) # creates an integer number from the float print("Temperature in Celsius (integer)", temp_cel_int) Using float on strings from math import radians angle_string = input("Enter a precise angle in degrees with decimal: ") angle_float = float(angle_string) radians_float = radians(angle_float) print("That angle in degrees is",radians_float,"in radians") More on reddit.com
🌐 r/learnpython
10
5
March 25, 2023
How to convert string to float ?
a='25$' b=float(''.join([i for i in a if i.isdigit()])) print(b) Use list iteration to take the digits and join the elements in the new list and convert to float value. More on reddit.com
🌐 r/learnpython
42
69
November 14, 2022
convert string to float?
if you really want to convert "1" of type str to 1 of type int then you can do something like that: ord("1")-48 this returns Unicode value of the digit, which is 48 for "0", 49 for "1", 50 for "2" etc. from that, it should be easy to create a floating point number in the for loop More on reddit.com
🌐 r/learnpython
16
2
September 22, 2022
🌐
Python Examples
pythonexamples.org › python-convert-int-to-float
Convert Int to Float in Python - Examples
#take an integer a = 5 print('Intput', a, type(a), sep='\n') #convert integer to float output = float(a) print('\nOutput', output, type(output), sep='\n') The variable a is assigned an integer value of 5. The float() function is called with a as its argument.
🌐
Manifoldapp
cuny.manifoldapp.org › read › how-to-code-in-python-3 › section › 517db09e-e5a2-4b02-bd3d-d5406a3b40a8
How To Convert Data Types | How To Code in Python 3 | Manifold @CUNY
That is, when you divide 5 by 2, in Python 3 you will get a float for an answer (2.5): ... In Python 2, since you were dealing with two integers, you would receive an integer back as your answer, instead: 5 / 2 = 2.
🌐
Snakify
snakify.org › integer and float numbers
Integer and float numbers - Learn Python 3 - Snakify
When we read an integer value, ... value can be written using a scientific notation. Eg., the distance from the Earth to the Sun is 1.496·1011, or 1.496e11 in Python....
🌐
Data Science Parichay
datascienceparichay.com › home › blog › python – convert integer to float
Python - Convert Integer to Float - Data Science Parichay
March 10, 2022 - # integer variable num = 12 # convert integer to float print(num*1.0) print(num/1.0) print(num+0.0) print(num-0.0) ... Subscribe to our newsletter for more informative guides and tutorials.
🌐
LabEx
labex.io › questions › how-to-convert-between-integer-and-float-in-python-271576
How to Convert Between Integer and Float in Python | LabEx
July 25, 2024 - To calculate the total cost of ... we convert the float value of 1.5 cups of sugar to an integer using the int() function, and then add it to the integer value of 2 cups of flour to get the total number of cups, which is 3....
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › converting large int value to float and back to int messes up the value?
r/learnpython on Reddit: Converting large int value to float and back to int messes up the value?
February 22, 2024 -

Say I have a large int value

>>> 7 ** 69
20500514515695490612229010908095867391439626248463723805607

Now let's say I convert it to a float value.

>>> float(7 ** 69)
2.050051451569549e+58

Seems about right. Now if I convert it back to back into an int...

>>> int(float(7 ** 69))
20500514515695489879650927979797501063745685669370929348608

Now it's a very different number. Why did that happen?

I am sorry if it's a stupid question but this has me very intrigued.

Top answer
1 of 11
35
There's a long-winded and very technical explanation about how floats and ints are implemented and why this happens for those numbers, but the reason why it must happen for some numbers is pretty simple: Python ints are unbounded, which means there are infinitely many ints you can have in Python. Python floats are 64-bit numbers, which means there are only 2^64 of them. Since there aren't infinitely many floats, you can't make int -> float -> int work for all numbers. The technical explanation is that ints are stored as a list of digits (essentially), so it can be as long as you want, but floats are basically a fraction, stored as a triple of numbers (s,p,q) each of a fixed-size and the numeric value of the float is (-1)^s*q*2^p. If this doesn't make sense, read up on "double-precision floating point" https://en.wikipedia.org/wiki/Double-precision_floating-point_format . The consequence of this is that floats can store very small and very large numbers, but there's a lot of gaps in between, and if your int falls in one of those gaps then when you turn it into a float with float() it picks the "nearest" one instead, so when you turn it back to an int you get the wrong value.
2 of 11
11
You've stumbled across the limitations of floating point numbers: https://docs.python.org/3/tutorial/floatingpoint.html If you look at the first 15 digits of the int and float, you'll see that the values are the same. This is because behind the scenes the float will only have 16 digits of precision due to how they are stored. Python has a library called decimals that can be used to improve precision. https://docs.python.org/3/library/decimal.html
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-convert-integers-to-floats-in-python-3
How To Convert Integers to Floats in Python 3 | DigitalOcean
September 18, 2020 - Python’s float() method will convert integers to floats. This is a quick tutorial on converting floats to integer numbers.
🌐
Reddit
reddit.com › r/learnpython › float and int function
r/learnpython on Reddit: Float and int function
March 25, 2023 -

Hello, please can someone explain to me when to use the float and int functions?

I mean should I use float when I am strictly dealing with decimal numbers, or when I want to convert whole numbers to decimal numbers?

pythonlearner

Top answer
1 of 4
5
float() and int() are mainly use to convert strings to numbers. They actually create a number from the strings. int() is also often use to create an integer number from a float number. A classic example is when you ask the user for a number. The input function always returns a string of character. To perform calculation you need a number (int or float). So you must create a number from that string. As in: string_number = input("Enter temperature in °F (integer): ") temp_far = int(string_number) temp_cel = (temp_far-32) * (5/9) # the result here is a float print("Temperature in celsius (float)", temp_cel) temp_cel_int = int(temp_cel) # creates an integer number from the float print("Temperature in Celsius (integer)", temp_cel_int) Using float on strings from math import radians angle_string = input("Enter a precise angle in degrees with decimal: ") angle_float = float(angle_string) radians_float = radians(angle_float) print("That angle in degrees is",radians_float,"in radians")
2 of 4
1
For string conversion, use the one appropriate to the string you're trying to convert. If you care about exact equality, don't use floats. Use integers, or maybe the Decimal class. If you want to know if floats are equal ish, keep in mind that the ish is always there. Use something like math.isclose to make such checks. There is rarely (almost never?) any need to explicitly convert integers to floats (unless you're doing something with numpy or similar). Integers will be automatically treated as floats during division (NOTE: different in some other languages), multiplication, etc. If you want to convert a float to integer, you can do that via int(2.3), but keep in mind how it rounds. There are other rounding functions that may or may not be more helpful (math.ceil, math.floor, round).
🌐
DataCamp
datacamp.com › tutorial › python-data-type-conversion
Python Data Type Conversion: A Guide With Examples | DataCamp
February 16, 2025 - You can learn more about them with DataCamp's Data Structures in Python Tutorial. Integers and floats are data types that deal with numbers. To convert an integer to a float, use the float() function in Python.
🌐
Vultr Docs
docs.vultr.com › python › built-in › float
Python float() - Convert To Floating Point | Vultr Docs
November 22, 2024 - ... numeric_string = "3.14" float_from_string = float(numeric_string) print(float_from_string) Explain Code · In this example, the string "3.14" is successfully converted to the floating-point number 3.14.
🌐
Tutorialspoint
tutorialspoint.com › python › python_numbers.htm
Python - Numbers
For example − ... However, look at the following assignment of the integer variable c. a = 10 b = 20 c = a + b print ("a:", a, "type:", type(a)) print ("c:", c, "type:", type(c)) ... Here, c is indeed an integer variable, but the expression ...
🌐
Processing
processing.org › examples › integersfloats.html
Integers Floats / Examples / Processing.org
*/ int a = 0; // Create a variable "a" of the datatype "int" float b = 0.0; // Create a variable "b" of the datatype "float" void setup() { size(640, 360); stroke(255); } void draw() { background(0); a = a + 1; b = b + 0.2; line(a, 0, a, height/2); ...
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-float-to-int-in-python
How to convert Float to Int in Python? - GeeksforGeeks
May 10, 2025 - For example, if a variable contains the value "33.28", we may want to convert it to a float to perform operations like addition or division. Let's explore dif ... Type Conversion is also known as typecasting, is an important feature in Python ...
🌐
Digiedutech
digiedutech.com › techhub › python › type-casting › float-function-python
Python float() Function: Convert Strings, Integers & Booleans
November 11, 2025 - Learn Python float() function to convert integers, strings & booleans into decimal numbers. Includes syntax, practical examples & error handling tips.
🌐
SwCarpentry
swcarpentry.github.io › python-novice-gapminder › 03-types-conversion.html
Plotting and Programming in Python: Data Types and Type Conversion
May 2, 2023 - Integer (int): represents positive or negative whole numbers like 3 or -512. Floating point number (float): represents real numbers like 3.14159 or -2.5. Character string (usually called “string”, str): text. Written in either single quotes or double quotes (as long as they match).
🌐
Python Guides
pythonguides.com › convert-float-to-int-python
How To Convert Float To Int In Python?
March 21, 2025 - You must first convert user input to float before converting to integer if the input might contain decimal points. Read How to Get File Name Without Extension in Python? In this article, I explained how to convert float to int in Python. I discussed eight important methods, such as using the int() function, the round() methods, and type conversion in calculation. I also discussed how to handle edge cases, comparison of methods, real-world examples, convert the user inputs, and some best practices.
🌐
DEV Community
dev.to › ken_mwaura1 › exploring-numeric-data-in-python-integers-floats-and-operations-2d9l
Exploring Numeric Data in Python: Integers, Floats, and Operations - DEV Community
January 5, 2024 - In this example, the addition operator (+) is used to add the integer x and the float y. The result is stored in the variable result. Lets explore the other arithmetic operations in Python.
🌐
W3Schools
w3schools.com › python › python_numbers.asp
Python Numbers
... x = 1 y = 35656222554887711 z = -3255522 print(type(x)) print(type(y)) print(type(z)) Try it Yourself » · Float, or "floating point number" is a number, positive or negative, containing one or more decimals.