Remove the Decimal part from a Float in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
5 min

banner

# Table of Contents

  1. Remove the decimal part from a Float using math.floor()
  2. Remove the decimal part from a List of Floats
  3. Remove the decimal part from a Float using int()
  4. Remove the decimal part from a Float using math.trunc()
  5. Remove the decimal part from a Float using math.ceil()
  6. Remove the decimal part from a Float using round()
  7. Remove the decimal part from a Float using split()
  8. Format a float as an integer in Python

# Remove the decimal part from a Float using math.floor()

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.

main.py
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

remove decimal part from float using math floor

The code for this article is available on GitHub

We used the math.floor() method to round a floating-point number down to the nearest integer.

main.py
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.

When passed a negative number, the math.floor() method rounds towards negative infinity.

This is different than the int() class, which rounds toward zero.

# Remove the decimal part from a List of Floats

You can use a list comprehension if you need to remove the decimal part from a list of floats.

main.py
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]

remove decimal part from list of floats

The code for this article is available on GitHub

We used a list comprehension to iterate over the list and then called the math.trunc() method with each float.

# Remove the decimal part from a Float using int()

Alternatively, you can use the int() class.

main.py
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

remove decimal part from float using int

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.

main.py
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.

If passed a floating-point number, the int() class truncates towards zero.
main.py
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 infinity
  • int() truncates towards zero

# Remove the decimal part from a Float using math.trunc()

If you need to round down, use the math.trunc() method.

main.py
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 code for this article is available on GitHub

The math.trunc() method takes a number, removes its fractional part and returns its integer part.

The math.trunc() method rounds towards zero.

main.py
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.

# Remove the decimal part from a Float using math.ceil()

You can also use the math.ceil() method to remove the decimal part from a float by rounding up.

main.py
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 code for this article is available on GitHub

The math.ceil() method returns the smallest integer greater than or equal to the provided number.

# Remove the decimal part from a Float using round()

You can also use the round() function to remove the decimal part from a float.

main.py
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:

NameDescription
numberthe number to round to ndigits precision after the decimal
ndigitsthe 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.

# Remove the decimal part from a Float using split()

You can also use the str.split() method to remove the decimal part from a float.

main.py
a_float = 3.14 a_list = str(a_float).split('.') print(a_list) integer_part = int(a_list[0]) print(integer_part) # ๐Ÿ‘‡๏ธ 3
The code for this article is available on GitHub

The str.split() method splits the string into a list of substrings using a delimiter.

The method takes the following 2 parameters:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done (optional)

We converted the float to a string and split the string on the period.

# Format a float as an integer in Python

If you need to format a float as an integer, use a formatted string literal.

main.py
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
The code for this article is available on GitHub

We used a formatted string literal to format a float as an integer.

The first example uses the math.trunc() method in a formatted string literal to remove the float's fractional part.
main.py
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.

main.py
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.

main.py
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.

main.py
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.

main.py
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.

main.py
import math result = f'{math.floor(378.9999999)}' print(result) # ๐Ÿ‘‰๏ธ 378
The code for this article is available on GitHub

The math.floor() method returns the largest integer less than or equal to the provided number.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev