Last modified: Feb 08, 2026 By Alexander Williams
Python Convert Float to Int | Easy Methods Guide
You often need to convert numbers in Python. Working with floats and integers is common. Converting a float to an integer is a key skill. This guide explains the best methods.
You will learn about the int() function. You will also see the math.floor() and math.ceil() functions. The round() function is useful too. Each method behaves differently.
Understanding these differences is crucial. It prevents errors in your calculations. It ensures your data is processed correctly. Let's explore each technique.
Using the int() Function for Truncation
The built-in int() function is the most direct way. It converts a float to an integer by truncation. Truncation means it removes the decimal part. It does not round the number.
It simply chops off everything after the decimal point. This is sometimes called "flooring towards zero." Let's look at an example.
# Example of using int() to convert float to int
float_num1 = 7.89
float_num2 = -3.14
int_num1 = int(float_num1)
int_num2 = int(float_num2)
print("Converted 7.89 to:", int_num1)
print("Converted -3.14 to:", int_num2)
Converted 7.89 to: 7
Converted -3.14 to: -3
Notice that 7.89 became 7. The decimal .89 was removed. For -3.14, it became -3. The .14 was removed. It did not become -4.
This is a key point. The int() function truncates towards zero. It's fast and simple. Use it when you just need the whole number part.
Sometimes your data starts as text. You may need to Python Convert String to Float first. Then you can use int() on the result.
Using math.floor() and math.ceil()
The math module offers more control. It provides math.floor() and math.ceil(). You must import the math module first.
math.floor() always rounds down. It returns the largest integer less than or equal to the float. math.ceil() always rounds up. It returns the smallest integer greater than or equal to the float.
import math
float_num = 5.7
floor_result = math.floor(float_num) # Rounds down
ceil_result = math.ceil(float_num) # Rounds up
print("Original float:", float_num)
print("math.floor(5.7):", floor_result)
print("math.ceil(5.7):", ceil_result)
# With a negative number
neg_float = -2.3
print("\nmath.floor(-2.3):", math.floor(neg_float))
print("math.ceil(-2.3):", math.ceil(neg_float))
Original float: 5.7
math.floor(5.7): 5
math.ceil(5.7): 6
math.floor(-2.3): -3
math.ceil(-2.3): -2
For 5.7, floor gives 5. Ceil gives 6. For -2.3, floor gives -3. Ceil gives -2. This is different from int() which gave -2 for -2.3.
Choose math.floor() for financial calculations. Use it when you must round down. Choose math.ceil() when you need to round up. This is common in pagination or resource allocation.
Using the round() Function
The round() function performs standard rounding. It rounds a float to the nearest integer. The .5 cases round to the nearest even number. This is called "bankers' rounding."
It can also round to a specific number of decimal places. For converting to int, you round to zero decimal places.
# Examples of the round() function
print("round(8.4):", round(8.4))
print("round(8.6):", round(8.6))
print("round(8.5):", round(8.5)) # .5 rounds to nearest even (8)
print("round(9.5):", round(9.5)) # .5 rounds to nearest even (10)
# You can then convert the result to an int explicitly
rounded_float = round(12.345)
final_int = int(rounded_float) # This is often redundant, as round() returns an int if ndigits is 0 or None.
print("\nFinal integer from round(12.345):", final_int)
round(8.4): 8
round(8.6): 9
round(8.5): 8
round(9.5): 10
Final integer from round(12.345): 12
round() is perfect for standard numerical rounding. It's what users expect. Remember the .5 rule. It might surprise you if you expect it to always round up.
After rounding, you have a float or an int. To ensure it's an integer, use int(). Or use our guide on Python Convert Number to String for output formatting.
Key Differences and Common Pitfalls
Choosing the wrong method causes bugs. Know the behavior of each function.
Truncation vs. Rounding:int() truncates. round() rounds. They give different results for numbers like 9.9.
Handling Negatives: This is critical. int(-2.7) gives -2. math.floor(-2.7) gives -3. Your choice depends on the application's logic.
Data Loss: Converting a float to an int loses the decimal part. Make sure this is acceptable for your task. You cannot get the decimal back from the integer.
Type Checking: Always know your data type. Use type(value) to check. This avoids unexpected errors in larger programs.
Practical Example: Processing a List of Floats
Let's apply this to a real task. You have a list of float measurements. You need a list of integers for a report.
sensor_readings = [23.456, 17.001, 19.999, -5.4, 0.0]
# Method 1: Truncate with int() using a list comprehension
truncated_readings = [int(x) for x in sensor_readings]
print("Truncated (int()):", truncated_readings)
# Method 2: Round to nearest integer
rounded_readings = [round(x) for x in sensor_readings]
print("Rounded (round()):", rounded_readings)
# Method 3: Always floor (for conservative estimates)
import math
floored_readings = [math.floor(x) for x in sensor_readings]
print("Floored (math.floor()):", floored_readings)
Truncated (int()): [23, 17, 19, -5, 0]
Rounded (round()): [23, 17, 20, -5, 0]
Floored (math.floor()): [23, 17, 19, -6, 0]
See how the results differ? For 19.999, int gives 19, round gives 20. For -5.4, int gives -5, floor gives -6.
The correct method depends on your project's needs. In graphics programming, you might do similar conversions when you Python Convert Images Between File Formats and handle pixel data.
Conclusion
Converting float to int in Python is straightforward. But you must pick the right tool.
Use int() for simple truncation towards zero. Use math.floor() to always round down. Use math.ceil() to always round up. Use round() for standard numerical rounding.
Remember the behavior with negative numbers. Test your logic with edge cases. This ensures your program works correctly.
Mastering these conversions is a fundamental step. It builds a strong foundation for more complex data tasks in Python. Just like knowing how to Python convert Sass to Css is key for web developers, knowing number conversion is key for Python programmers.