In this tutorial, we will look at how to convert an integer (
int datatype) in Python to a float (
float datatype) with the help of some examples.
How to convert int to float in Python?
You can use the Python built-in
float() function to convert an integer to a float. Pass the integer you want to convert as an argument. The following is the syntax –
# convert integer i to float float(i)
It returns the number as a float value.
Let’s look at some examples of using the above syntax to convert an integer value to a float.
Here we apply the
float() function on an integer value
# integer variable num = 12 # convert integer to float num = float(num) # display the number and its type print(num) print(type(num))
12.0 <class 'float'>
You can see that the variable
num is of
float type now with the value
Alternatively, you can perform an arithmetic identity operation on the integer with a float value to get the resulting value as an integer. For example, multiply or divide the integer by
1.0 or add or subtract
0.0 to get the same value but as a float.
Here’s an example –
# integer variable num = 12 # convert integer to float print(num*1.0) print(num/1.0) print(num+0.0) print(num-0.0)
12.0 12.0 12.0 12.0
You can see that all the above operations result in the float value
You might also be interested in –
- Extract Numbers From String in Python
- Python – Check if String Contains Only Numbers
- Python – Convert Integer to String
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.