Integers in Python are wholesome numbers, whereas float values are numbers with decimals.
If you are working on a number-sensitive program like trading software, you need precision in decimal points, too. Therefore, if our calculations might result in fractional values, we will need a floating value that provides greater accuracy and flexibility.
The easiest and most efficient way to convert an integer to a float (double) value is using the “float()” constructor. It accepts an integer value and returns the float.
num_int = 21 print(num_int) # Output: 21 print(type(num_int)) # Output: <class 'int'> # Conversion num_float = float(num_int) print(num_float) # Output: 21.0 print(type(num_float)) # Output: <class 'float'>
We verified the conversion using the type() function that returns the type of variables.
Implicit conversion in arithmetic operations
If you are performing an arithmetic operation and your one operand is an integer and another is a float, Python implicitly promotes the integer to a float in the output.
num_int = 21 num_float = 19.21 # Arithmetic operation of summation sum = num_int + num_float print(sum) # Output: 40.21 print(type(sum)) # Output: <class 'float'>
This float conversion happens automatically and implicitly.
Division and float promotion
If you perform a division operation using “/“, it always returns the float value, even when dividing integers.
num_int = 21 another_int = 7 # Arithmetic operation of division division = num_int / another_int print(division) # Output: 3.0 print(type(division)) # Output: <class 'float'>
The above code shows that even if 21 and 7 are pure integers, the output is in float format because of the “/” operator.
Converting a list of integers to floats
Real-life application mostly has a collection of integers in the format of a list. In that scenario, you can use list comprehension to convert that list of integers to a list of floats with the help of the float() function.
int_list = [21, 19, 18, 20] print(int_list) # Output: [21, 19, 18, 20] # Using list comprehension to convert the list of integers to a list of floats float_list = [float(x) for x in int_list] print(float_list) # Output: [21.0, 19.0, 18.0, 20.0]
Converting booleans
Booleans are subclass of integers which means 1 is equal to True and 0 is equal to False.
- If you convert True to float, it returns 1.0.
- If you convert False to float value, it returns 0.0.
print(float(True)) # Output: 1.0 print(float(False)) # Output: 0.0
Converting an array of integers
If you are computing numerical values, you might use the Numpy library, which provides an array type.
If your input array is filled with integers, you can use the .astype() function to convert it into an array of floats.
import numpy as np int_array = np.array([11, 21, 31]) print(int_array) # Output: [11 21 31] # Conversion using .astype() method float_array = int_array.astype('float64') print(float_array) # Output: [11. 21. 31.]
Custom Objects with __float__()
If your requirement is to customize the float conversion, you can define the built-in private method “__float__()” method.
class CustomNumber: def __init__(self, value): self.value = value def __float__(self): return float(self.value) obj = CustomNumber(21) float_obj = float(obj) print(float_obj) # Output: 21.0 print(type(float_obj)) # Output: <class 'float'>
Precision loss with large integers
In Python, floats have limited precision (64-bit double-precision), so converting very large integers may cause rounding errors.
large_int = 2**53 float_num = float(large_int) print(large_int == float_num) # Output: True (no loss) larger_int = 2**54 + 1 floater_num = float(larger_int) print(floater_num == larger_int) # Output: False (precision loss)
That’s all!