Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert float to integer in Python?
In Python there are two main numeric data types: integers and floats. Integers are whole numbers without decimal points, while floats contain decimal values. Python provides several built-in methods to convert float values to integers ?
Using the int() Function
The int() function converts floating-point numbers to integers by truncating the decimal part. It does not round values − it simply removes everything after the decimal point.
Basic Float to Integer Conversion
Here's a simple example of converting a float to an integer ?
num = 39.98
print('Data type of num:', type(num).__name__)
# float to int conversion
num = int(num)
print('Converted value and its data type:', num, ', type:', type(num).__name__)
Data type of num: float Converted value and its data type: 39 , type: int
Converting Float Strings
The int() function cannot directly convert a string containing a float. You must first convert the string to float, then to integer ?
# This will raise a ValueError
try:
num_str = '1.5'
result = int(num_str)
except ValueError as e:
print(f"Error: {e}")
# Correct approach - convert string to float first
num_str = '1.5'
result = int(float(num_str))
print(f"Converted value: {result}")
Error: invalid literal for int() with base 10: '1.5' Converted value: 1
Floating-Point Precision Edge Case
Due to floating-point precision limitations, some values may appear to round unexpectedly ?
num = 1.9999999999999999
print('Original value:', num)
print('Data type:', type(num).__name__)
# float to int conversion
converted = int(num)
print('Converted value:', converted, ', type:', type(converted).__name__)
Original value: 2.0 Data type: float Converted value: 2 , type: int
Using Math Module Functions
The math module provides additional functions for converting floats to integers with different behaviors.
Using math.trunc()
The trunc() function truncates toward zero, similar to int() ?
import math
positive_num = 7.89
negative_num = -7.89
print('Positive:', math.trunc(positive_num))
print('Negative:', math.trunc(negative_num))
Positive: 7 Negative: -7
Using math.floor() and math.ceil()
These functions provide floor (round down) and ceiling (round up) conversions ?
import math
num = 7.3
print('Original:', num)
print('Floor (round down):', math.floor(num))
print('Ceiling (round up):', math.ceil(num))
print('Truncate:', math.trunc(num))
Original: 7.3 Floor (round down): 7 Ceiling (round up): 8 Truncate: 7
Comparison of Methods
| Method | Behavior | Example (7.9) | Example (-7.9) |
|---|---|---|---|
int() |
Truncates toward zero | 7 | -7 |
math.trunc() |
Truncates toward zero | 7 | -7 |
math.floor() |
Always rounds down | 7 | -8 |
math.ceil() |
Always rounds up | 8 | -7 |
Conclusion
Use int() for basic float-to-integer conversion with truncation. Use math.floor() or math.ceil() when you need specific rounding behavior. Remember to convert float strings to float first before applying int().
