Recently while working on a project for my clients, I encountered a scenario where I needed to resize images, pixel coordinates must be whole numbers because pixels cannot be fractional. Then explored more about converting float to int. In this article, I will explain how to convert float to int in Python with suitable examples.
Convert Float to Int in Python
Let us learn how to convert float to int in Python using various methods with examples.
Read How to Unzip a File in Python?
Method 1: Use the int() Function (Basic Truncation)
The most simple way to convert a float to an integer in Python is by using the built-in int() function.
float_number = 7.85
integer_number = int(float_number)
print(integer_number) Output:
7You can refer to the below screenshot to see the output.

The int() function truncates the decimal part and returns only the integer portion of the float. This means it always rounds toward zero, discarding any decimal values.
Check out How to Read Tab-Delimited Files in Python?
Method 2: Rounding Methods
Sometimes simply truncating a float isn’t what you want. Python offers several ways to round numbers before converting them to integers.
Use round() Function
float_number = 7.85
rounded_number = round(float_number)
integer_number = int(rounded_number) # Or simply: int(round(float_number))
print(integer_number) Output:
8You can refer to the below screenshot to see the output.

The round() function rounds to the nearest integer. If the decimal part is exactly 0.5, Python rounds to the nearest even number (banker’s rounding).
Use math.floor() and math.ceil()
To have more control over the rounding direction, you can use the math module:
import math
float_number = 7.85
# Floor (round down)
floor_number = math.floor(float_number)
print(floor_number)
# Ceiling (round up)
ceil_number = math.ceil(float_number)
print(ceil_number)Output:
7
8You can refer to the below screenshot to see the output.

The safest way to get an integer from math.floor() is to pipe it through int(), through math.floor() already returns an integer in Python 3.
Read How to Split a File into Multiple Files in Python?
Method 3: Type Conversion in Calculations
You can also use type conversion such as converting floats to integers during calculations:
float_number = 7.85
# Using integer division
integer_number = int(float_number // 1)
print(integer_number) # Output: 7
# Using multiplication and division
integer_number = int(float_number * 1)
print(integer_number) # Output: 7By converting float to int it removes the decimal numbers and returns the number as shown in the above example.
Check out How to Skip the First Line in a File in Python?
Handle Edge Cases
Let us learn how to handle some common cases:
Very Large Numbers
large_float = 1e20
large_int = int(large_float)
print(large_int) # 100000000000000000000For extremely large numbers, be cautious about potential precision loss.
Negative Numbers
The int() function always truncates toward zero, which behaves differently for negative numbers than math.floor():
negative_float = -7.85
print(int(negative_float)) # Output: -7 (truncates toward zero)
print(math.floor(negative_float)) # Output: -8 (rounds down)Convert in NumPy Arrays
If you’re working with NumPy arrays, you can convert all float elements to integers:
import numpy as np
float_array = np.array([1.5, 2.7, 3.9])
int_array = float_array.astype(int)
print(int_array) # Output: [1 2 3]Read How to Read XML Files in Python?
Comparison of Methods
Here’s a quick comparison of the different methods:
| Method | Behavior | Use When |
|---|---|---|
int() | Truncates toward zero | You need simple truncation |
round() then int() | Rounds to nearest integer | You need standard rounding |
math.floor() | Rounds down | You need to always round down |
math.ceil() | Rounds up | You need to always round up |
// operator | Integer division | You’re already doing calculations |
Check out How to Clear a File in Python?
Best Practices for Float to Int Conversion
- Be explicit about your rounding intentions. Don’t assume that
int()will round the way you want. - Document your conversion logic, especially when it affects business calculations.
- Consider edge cases such as negative numbers and very large values.
- Type-check inputs when necessary to ensure you’re converting the right types.
Read How to Write Lines to a File in Python?
Examples
Let us understand more about this conversion by considering real-world examples.
Example 1: Financial Calculations
Let us consider that you’re calculating the total cost of items in a shopping cart:
item_price = 19.99
quantity = 3
total_cost = item_price * quantity # 59.97
# If you need to display as cents:
total_cents = int(total_cost * 100) # 5997Example 2: Data Processing
When analyzing datasets, you might need to bin continuous values:
ages = [22.7, 19.3, 35.8, 41.2, 28.5]
age_groups = [int(age // 10 * 10) for age in ages]
print(age_groups) # [20, 10, 30, 40, 20]Check out How to Write Multiple Lines to a File in Python?
Convert User Input
When collecting numeric input from users, you’ll often need to convert strings to floats and then possibly to integers:
user_input = input("Enter a number: ") # "7.85"
try:
# First convert to float
float_number = float(user_input)
# Then convert to integer
integer_number = int(float_number)
print(f"Your number as an integer: {integer_number}")
except ValueError:
print("Please enter a valid number")You must first convert user input to float before converting to integer if the input might contain decimal points.
Read How to Get File Name Without Extension in Python?
Conclusion
In this article, I explained how to convert float to int in Python. I discussed eight important methods, such as using the int() function, the round() methods, and type conversion in calculation. I also discussed how to handle edge cases, comparison of methods, real-world examples, convert the user inputs, and some best practices.
You may read:
- How to Check if a File is Empty in Python?
- How to Rename Files in Python?
- How to Overwrite a File in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.