How to convert Float to Int in Python?
In Python, you can convert a float to an integer using type conversion. This process changes the data type of a value However, such conversions may be lossy, as the decimal part is often discarded.
For example:
- Converting 2.0 (float) to 2 (int) is safe because here, no data is lost.
- But converting 3.4 results in 3 in which the fractional part is lost.
Let's explore different methods of doing it in Python:
Method 1: Conversion using int():
To convert a float value to int we make use of the built-in int() function, this function trims the values after the decimal point and returns only the integer/whole number part.
Example 1: Simple conversion of float to int:
n = 9.3
print('Original type:', type(n).__name__)
n = int(n)
print('Converted value:', n, ', Type:', type(n).__name__)
Output
Original type: float Converted value: 9 , Type: int
Explanation:
- 9.3 is a float.
- int(num) removes .3 and returns 9.
- Type changes from float to int.
Example 2: Precision Conversion
The int() function in Python removes the decimal part and keeps only the whole number. However, due to how floating-point numbers are stored in memory, a value like 5.99999999999999999999 might be treated as 6.0. So, converting it to an integer gives 6.
To avoid such precision issues, we can use the decimal module for better accuracy or math.floor() to always round down.
n1 = 5.9
n2 = 5.99999999999999999999
print(int(n1))
print(int(n2))
Output
5 6
Explanation:
- num2 appears to be less than 6, but due to floating-point precision, it's stored as 6.0.
- int(num2) becomes 6.
Method 2: Using math.floor() and math.ceil()
A float value can be converted to an int value no larger than the input by using the math.floor() function, whereas it can also be converted to an int value which is the smallest integer greater than the input using math.ceil() function. The math module is to be imported in order to use these methods.
Example : In the below example conversion from float to int has been achieved using the floor() and ceil() methods, the former returns an int no larger than the input and the latter returns the smallest integer larger than the input.
import math
n = 5.6
f = math.floor(n)
c = math.ceil(n)
print("Using floor():", f, ', Type:', type(f).__name__)
print("Using ceil() :", f, ', Type:', type(c).__name__)
Output
Using floor(): 5 , Type: int Using ceil() : 5 , Type: int
Explanation:
- math.floor(5.6) returns 5 (rounds down).
- math.ceil(5.6) returns 6 (rounds up).
- Both return int types.
Method 3: Using round()
A float value can be converted to an int value which is closet integer value if does not pass second parameter. In case of equal difference it goes toward larger integer.
Example: In the below example conversion from float to int has been achieved using the round() methods, the former returns an int number which is closest to number.
n = 5.6
print('Original type:', type(n).__name__)
print('Original number:', n)
val = round(n)
print('Rounded type:', type(val).__name__)
print('Rounded value:', val)
Output
Original type: float Original number: 5.6 Rounded type: int Rounded value: 6
Explanation:
- round(5.6) returns 6 ,closest integer.
- If number ends in .5, Python rounds to the nearest even number.
Method 4: Using math.trunc()
The math.trunc() function truncates the float — similar to int(), but comes from the math module.
Example: In the below example conversion from float to int has been achieved using the math.trunc() methods, the former returns an larger int number which in case of negative number, else in case of positive number return smaller integer number.
import math
n1 = 5.6
n2 = -2.6
# Positive number
data = math.trunc(n1)
print("Truncated (positive):", data, ', Type:', type(data).__name__)
# Negative number
val = math.trunc(n2)
print("Truncated (negative):", val, ', Type:', type(val).__name__)
Output
Truncated (positive): 5 , Type: int Truncated (negative): -2 , Type: int
Explanation:
- math.trunc(5.6) returns 5 (cuts off decimal).
- math.trunc(-2.6) returns -2 (still just removes the fraction).
- Behaves like int(), but from math.