Python How to Convert Float to Integer Easily
In Python, you can convert a float to an integer by using the
int() function, like int(3.7), which will return 3.Examples
Input3.7
Output3
Input-2.9
Output-2
Input0.0
Output0
How to Think About It
To convert a float to an integer, think about removing the decimal part. Using the
int() function in Python simply cuts off everything after the decimal point without rounding, giving you the integer part.Algorithm
1
Get the float number as input.2
Use the <code>int()</code> function to convert the float to an integer.3
Return or print the integer result.Code
python
float_num = 3.7 int_num = int(float_num) print(int_num)
Output
3
Dry Run
Let's trace converting 3.7 to an integer using int()
1
Start with float
float_num = 3.7
2
Convert to int
int_num = int(3.7) which becomes 3
3
Print result
print(3) outputs 3
| Step | Value |
|---|---|
| Initial float | 3.7 |
| After int() | 3 |
| Output | 3 |
Why This Works
Step 1: int() removes decimal
The int() function cuts off the decimal part without rounding.
Step 2: Conversion returns integer
The result is a whole number type, not a float.
Alternative Approaches
Using math.floor()
python
import math float_num = 3.7 int_num = math.floor(float_num) print(int_num)
This always rounds down to the nearest integer, unlike int() which truncates.
Using round() and then int()
python
float_num = 3.7 int_num = int(round(float_num)) print(int_num)
This rounds the float to the nearest integer before converting.
Complexity: O(1) time, O(1) space
Time Complexity
Conversion with int() is a simple operation that takes constant time.
Space Complexity
No extra memory is needed besides the output integer.
Which Approach is Fastest?
int() is fastest for truncation; math.floor() and round() add slight overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| int() | O(1) | O(1) | Simple truncation without rounding |
| math.floor() | O(1) | O(1) | Always rounding down |
| round() + int() | O(1) | O(1) | Rounding to nearest integer |
Use
int() to quickly drop the decimal part of a float.Expecting
int() to round the number instead of just truncating the decimal part.