Python How to Convert Float to Int Without Rounding
To convert a float to an int without rounding in Python, use
int(your_float) or math.trunc(your_float), which simply remove the decimal part without rounding.Examples
Input3.7
Output3
Input-2.9
Output-2
Input5.0
Output5
How to Think About It
To convert a float to an int without rounding, think about removing the decimal part completely instead of changing the number up or down. Using
int() or math.trunc() cuts off everything after the decimal point, keeping only the whole number part.Algorithm
1
Get the float number as input.2
Remove the decimal part without rounding by truncating it.3
Return the resulting integer.Code
python
import math float_num = 3.7 int_num = int(float_num) # or use math.trunc(float_num) print(int_num)
Output
3
Dry Run
Let's trace converting 3.7 to int without rounding.
1
Input float
float_num = 3.7
2
Convert using int()
int_num = int(3.7) which becomes 3
3
Print result
Output is 3
| Step | Value |
|---|---|
| Input float | 3.7 |
| After int() | 3 |
| Output | 3 |
Why This Works
Step 1: int() truncates decimals
The int() function removes the decimal part of a float without rounding, simply keeping the integer portion.
Step 2: math.trunc() does the same
The math.trunc() function also truncates the decimal part and returns the integer part, useful for clarity.
Alternative Approaches
Using math.trunc()
python
import math float_num = 3.7 int_num = math.trunc(float_num) print(int_num)
Explicitly shows truncation, good for readability.
Using string split
python
float_num = 3.7 int_num = int(str(float_num).split('.')[0]) print(int_num)
Converts float to string and splits at decimal, less efficient but shows manual truncation.
Complexity: O(1) time, O(1) space
Time Complexity
Conversion using int() or math.trunc() is a constant time operation, O(1), as it only processes one number.
Space Complexity
No extra memory is needed beyond storing the input and output, so space complexity is O(1).
Which Approach is Fastest?
int() is the fastest and simplest method; math.trunc() is equally fast but more explicit; string splitting is slower and less efficient.
| Approach | Time | Space | Best For |
|---|---|---|---|
| int() | O(1) | O(1) | Simple and fast truncation |
| math.trunc() | O(1) | O(1) | Explicit truncation with math module |
| String split | O(n) | O(n) | Manual truncation, less efficient |
Use
int() to quickly drop decimals without rounding.Using
round() instead of int() which rounds the number instead of truncating.