How to Remove the Decimal Part of Floats in Python
This guide explores various methods for removing the decimal part of floating-point numbers in Python, effectively converting them to integers. We'll cover techniques using math.trunc()
, int()
, math.floor()
, math.ceil()
, round()
, and string manipulation (with split()
), highlighting the differences between them and recommending best practices.
Truncating with math.trunc()
(Recommended)
The math.trunc()
function removes the decimal part of a float, returning the integer part towards zero. This is generally the best approach for simply removing the decimal without rounding:
import math
result_1 = math.trunc(3.999)
print(result_1) # Output: 3
result_2 = math.trunc(3.14)
print(result_2) # Output: 3
result_3 = math.trunc(-3.6)
print(result_3) # Output: -3
math.trunc()
always rounds towards zero, regardless of whether the number is positive or negative.
Truncating with int()
The built-in int()
function also truncates towards zero, making it functionally equivalent to math.trunc()
for this specific task:
result_1 = int(3.999)
print(result_1) # Output: 3
result_2 = int(3.14)
print(result_2) # Output: 3
result_3 = int(-3.6)
print(result_3) # Output: -3
Both math.trunc()
and int()
remove the fractional part, effectively rounding towards zero. This is different from rounding down (towards negative infinity).
Rounding Down with math.floor()
The math.floor()
function rounds a number down to the nearest integer (towards negative infinity):
import math
result_1 = math.floor(3.999)
print(result_1) # Output: 3
result_2 = math.floor(3.14)
print(result_2) # Output: 3
result_3 = math.floor(-3.6)
print(result_3) # Output: -4 (Rounds DOWN to -4)
- This will return a lower integer value if a negative float is passed in.
Rounding Up with math.ceil()
The math.ceil()
function rounds a number up to the nearest integer (towards positive infinity):
import math
result_1 = math.ceil(3.999)
print(result_1) # Output: 4
result_2 = math.ceil(3.14)
print(result_2) # Output: 4
result_3 = math.ceil(-3.6)
print(result_3) # Output: -3
Rounding to Nearest Integer with round()
The round()
function rounds to the nearest integer (or to a specified number of decimal places):
result_1 = round(3.999)
print(result_1) # Output: 4
result_2 = round(3.14)
print(result_2) # Output: 3
result_3 = round(-3.6)
print(result_3) # Output: -4
String Manipulation with split()
(Less Recommended)
While possible, using string manipulation is generally not the recommended way to remove the decimal part, as it's less efficient and can be prone to errors with different float representations. However, for completeness, here's how it can be done:
a_float = 3.14
a_list = str(a_float).split('.')
print(a_list) # Output: ['3', '14']
integer_part = int(a_list[0])
print(integer_part) # Output: 3
- The code will split the float into a list of strings by the dot character.
- Then, the first part is taken from the list and converted to an integer.
This approach should be avoided when precision is important, as it will lead to rounding errors.
Formatting Floats as Integers (String Conversion)
If your goal is to display a float as an integer (without actually changing its underlying numerical value), use f-string formatting or the str.format()
:
import math
my_float = 378.656789
# Using truncating
result = f'{math.trunc(my_float)}' # Remove the decimal, no rounding
print(result) # Output: 378
# Using standard rounding
result = f'{my_float:.0f}'
print(result) # Output: 379
# Using rounding up
result = f'{math.ceil(378.000001)}'
print(result) # Output: 379
# Using rounding down
result = f'{math.floor(378.999999)}'
print(result) # Output: 378
- These examples convert a floating point number to an integer, using different approaches, for formatting/display purposes.
Removing Decimal Parts from a List of Floats
To remove the decimal part from a list of floats, use a list comprehension with math.trunc()
(or int()
) :
import math
list_of_floats = [3.999, 3.14, 5.71]
new_list = [math.trunc(x) for x in list_of_floats]
print(new_list) # Output: [3, 3, 5]
new_list = [int(x) for x in list_of_floats]
print(new_list) # Output: [3, 3, 5]