Last updated: Apr 9, 2024
Reading timeยท5 min
Use the math.floor()
method to remove the decimal part from a float, e.g.
result = math.floor(3.999)
.
The math.floor
method returns the largest integer less than or equal to the
provided number.
import math result_1 = math.floor(3.999) print(result_1) # ๐๏ธ 3 result_2 = math.floor(3.14) print(result_2) # ๐๏ธ 3 result_3 = math.floor(-3.6) print(result_3) # ๐๏ธ -4
We used the math.floor()
method to round a floating-point number down to the
nearest integer.
import math print(math.floor(6.99)) # ๐๏ธ 6 print(math.floor(6.01)) # ๐๏ธ 6 print(math.floor(-6.01)) # ๐๏ธ -7
The math.floor() method returns the largest integer less than or equal to the provided number.
math.floor()
method rounds towards negative infinity.This is different than the int() class, which rounds toward zero.
You can use a list comprehension if you need to remove the decimal part from a list of floats.
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) # ๐๏ธ [3, 3, 5]
We used a
list comprehension to
iterate over the list and then called the math.trunc()
method with each float.
Alternatively, you can use the int()
class.
result_1 = int(3.999) print(result_1) # ๐๏ธ 3 result_2 = int(3.14) print(result_2) # ๐๏ธ 3 result_3 = int(-3.6) print(result_3) # ๐๏ธ -3
The int()
class truncates floating-point numbers toward zero, so it will
return an int
that represents the number without the decimal places.
If you need to remove the decimal part from a list of floats, use a list comprehension.
list_of_floats = [3.999, 3.14, 5.71] new_list = [int(x) for x in list_of_floats] print(new_list) # ๐๏ธ [3, 3, 5]
The int class returns an integer object constructed from the provided number or string argument.
int()
class truncates towards zero.print(int(-11.1)) # ๐๏ธ -11 print(int(-13.9)) # ๐๏ธ -13
Notice that there is a difference in how math.floor()
and int()
handle
negative numbers:
math.floor()
rounds towards negative infinityint()
truncates towards zeroIf you need to round down, use the math.trunc()
method.
import math result_1 = math.trunc(3.999) print(result_1) # ๐๏ธ 3 result_2 = math.trunc(3.14) print(result_2) # ๐๏ธ 3 result_3 = math.trunc(-3.6) print(result_3) # ๐๏ธ -3
The math.trunc() method takes a number, removes its fractional part and returns its integer part.
The math.trunc()
method rounds towards zero.
import math print(math.trunc(-9.1)) # ๐๏ธ -9 print(math.trunc(-9.9)) # ๐๏ธ -9
This approach achieves the same result as passing the floating-point number to
the int()
class.
You can also use the math.ceil()
method to remove the decimal part from a
float by rounding up.
import math result_1 = math.ceil(3.999) print(result_1) # ๐๏ธ 4 result_2 = math.ceil(3.14) print(result_2) # ๐๏ธ 4 result_3 = math.ceil(-3.6) print(result_3) # ๐๏ธ -3
The math.ceil() method returns the smallest integer greater than or equal to the provided number.
You can also use the round()
function to remove the decimal part from a float.
result_1 = round(3.999) print(result_1) # ๐๏ธ 4 result_2 = round(3.14) print(result_2) # ๐๏ธ 3 result_3 = round(-3.6) print(result_3) # ๐๏ธ -4
The round function takes the following 2 parameters:
Name | Description |
---|---|
number | the number to round to ndigits precision after the decimal |
ndigits | the number of digits after the decimal, the number should have after the operation (optional) |
The round
function returns the number rounded to ndigits
precision after the
decimal point.
If ndigits
is omitted, the function returns the nearest integer.
You can also use the str.split()
method to remove the decimal part from a
float.
a_float = 3.14 a_list = str(a_float).split('.') print(a_list) integer_part = int(a_list[0]) print(integer_part) # ๐๏ธ 3
The str.split() method splits the string into a list of substrings using a delimiter.
The method takes the following 2 parameters:
Name | Description |
---|---|
separator | Split the string into substrings on each occurrence of the separator |
maxsplit | At most maxsplit splits are done (optional) |
We converted the float to a string and split the string on the period.
If you need to format a float as an integer, use a formatted string literal.
import math my_float = 378.656789 # โ format float as int by removing fractional part (truncating) result = f'{math.trunc(my_float)}' print(result) # ๐๏ธ 378 # ---------------------------------------------- # โ format float as int with rounding result = f'{my_float:.0f}' print(result) # ๐๏ธ 379 # ---------------------------------------------- # โ format float as int by rounding up result = f'{math.ceil(378.000001)}' print(result) # ๐๏ธ 379
We used a formatted string literal to format a float as an integer.
math.trunc()
method in a formatted string literal to remove the float's fractional part.import math my_float = 378.656789 result = f'Result: {math.trunc(my_float)}' print(result) # ๐๏ธ Result: 378
The math.trunc method takes a number, removes its fractional part and returns its integer part.
The math.trunc()
method rounds towards zero.
import math print(math.trunc(3.468)) # ๐๏ธ 3 print(math.trunc(-3.468)) # ๐๏ธ -3
Formatted string literals (f-strings) let us include expressions inside of a
string by prefixing the string with f
.
my_str = 'The number is:' my_int = 123 result = f'{my_str} {my_int}' print(result) # ๐๏ธ The number is: 123
Make sure to wrap expressions in curly braces - {expression}
.
Formatted string literals also enable us to use the format-specific mini-language in expression blocks.
my_float = 378.656789 result = f'{my_float:.0f}' print(result) # ๐๏ธ 379
The digit after the period is the number of decimal places the number should have.
Notice that f-strings format with rounding.
You can use the math.ceil()
method to round up when formatting the float as an
integer.
import math result = f'{math.ceil(378.000001)}' print(result) # ๐๏ธ 379
The math.ceil() method returns the smallest integer greater than or equal to the provided number.
If you need to round down, use the math.floor()
method.
import math result = f'{math.floor(378.9999999)}' print(result) # ๐๏ธ 378
The math.floor() method returns the largest integer less than or equal to the provided number.
You can learn more about the related topics by checking out the following tutorials: