Of course! Converting a float to an int in Python is a common task. The primary and most direct way is to use the built-in int() function.

However, the most important thing to understand is how Python handles the decimal part. It does not round the number; it truncates it, which means it simply chops off everything after the decimal point.
Let's break it down.
The Basic Method: int()
This is the standard way to convert a float to an integer.
How it Works: Truncation
int() always truncates towards zero. This means:

- For a positive number, it removes the decimal part (rounds down).
- For a negative number, it also removes the decimal part (which effectively rounds up towards zero).
Examples
# --- Positive Numbers ---
positive_float = 8.9
positive_int = int(positive_float)
print(f"Original float: {positive_float}")
print(f"Converted int: {positive_int}")
# Output:
# Original float: 8.9
# Converted int: 8 (The .9 is chopped off)
# --- Negative Numbers ---
negative_float = -8.9
negative_int = int(negative_float)
print(f"\nOriginal float: {negative_float}")
print(f"Converted int: {negative_int}")
# Output:
# Original float: -8.9
# Converted int: -8 (The .9 is chopped off, moving towards zero)
# --- Numbers with no decimal part ---
float_no_decimal = 42.0
int_no_decimal = int(float_no_decimal)
print(f"\nOriginal float: {float_no_decimal}")
print(f"Converted int: {int_no_decimal}")
# Output:
# Original float: 42.0
# Converted int: 42
The "Gotcha": Rounding vs. Truncation
A common mistake is assuming int() rounds the number. It does not. If you need to round to the nearest integer, you must use the round() function first.
Example of the Mistake
my_float = 7.8
# INCORRECT if you want to round: This truncates to 7
truncated_value = int(my_float)
print(f"Truncated with int(): {truncated_value}") # Output: 7
# CORRECT for rounding: This rounds to the nearest integer, 8
rounded_value = round(my_float)
print(f"Rounded with round(): {rounded_value}") # Output: 8
# Another example
my_float_2 = 7.2
truncated_value_2 = int(my_float_2)
print(f"Truncated with int(): {truncated_value_2}") # Output: 7
rounded_value_2 = round(my_float_2)
print(f"Rounded with round(): {rounded_value_2}") # Output: 7
Other Conversion Methods
While int() is the most common, there are other ways to convert, each with a specific purpose.
math.floor(): Rounding Down
The math.floor() function always rounds a number down to the nearest integer, regardless of whether it's positive or negative. This is different from int() for negative numbers.
You need to import math first.

import math
# --- Positive Number ---
positive_float = 8.9
floor_positive = math.floor(positive_float)
int_positive = int(positive_float)
print(f"Original float: {positive_float}")
print(f"math.floor(): {floor_positive}") # Rounds down to 8
print(f"int(): {int_positive}") # Truncates to 8
# Result is the same for positive numbers
# --- Negative Number ---
negative_float = -8.9
floor_negative = math.floor(negative_float)
int_negative = int(negative_float)
print(f"\nOriginal float: {negative_float}")
print(f"math.floor(): {floor_negative}") # Rounds DOWN to -9
print(f"int(): {int_negative}") # Truncates TOWARDS ZERO to -8
# Result is DIFFERENT for negative numbers
math.ceil(): Rounding Up
The math.ceil() function (short for "ceiling") always rounds a number up to the nearest integer.
import math
my_float = 8.1
ceiling_value = math.ceil(my_float)
print(f"Original float: {my_float}")
print(f"math.ceil(): {ceiling_value}") # Output: 9
my_float_2 = -8.1
ceiling_value_2 = math.ceil(my_float_2)
print(f"\nOriginal float: {my_float_2}")
print(f"math.ceil(): {ceiling_value_2}") # Output: -8
Summary Table
| Method | What It Does | Example (9) |
Example (-8.9) |
Best For... |
|---|---|---|---|---|
int(x) |
Truncates towards zero (chops off decimals) | 8 |
-8 |
Getting the integer part of a number when you don't care about rounding. |
round(x) |
Rounds to the nearest integer (.5 rounds to the nearest even number) |
9 |
-9 |
When you need standard mathematical rounding. |
math.floor(x) |
Rounds down to the nearest integer | 8 |
-9 |
When you always need to round down (e.g., calculating how many whole items fit in a box). |
math.ceil(x) |
Rounds up to the nearest integer | 9 |
-8 |
When you always need to round up (e.g., calculating how many whole containers you need). |
When to Use Which?
- Use
int()when you simply want to discard the fractional part of a positive number, or when you are working with numbers and need their integer representation without rounding. - Use
round()when you need to perform standard mathematical rounding to the nearest whole number. - Use
math.floor()when you need to ensure the result is the largest integer less than or equal to the original number (e.g., for pagination, calculating floors in a building). - Use
math.ceil()when you need to ensure the result is the smallest integer greater than or equal to the original number (e.g., for calculating how many items to order, ensuring you have enough).
