Open In App

How to remove all decimals from a number using Python?

Last Updated : 27 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We have to remove all decimals from a number and get the required output using Python. There are several ways to achieve this, depending on whether you want to simply truncate the decimal part, round it or perform other manipulations. For example, number is 12.345 then result is 12.

Using int( )

int( ) is a built-in function used to convert any value into an integer number. 

Python
a = 44.560
b = 856.9785623
c = 9999.99

# Type conversion float to int
x = int(a)
y = int(b)
z = int(c)

print("Number1 = ", x)
print("Number2 = ", y)
print("Number3 = ", z)

# type() function returns the data type
print(type(a))
print(type(x))

Output
Number1 =  44
Number2 =  856
Number3 =  9999
<class 'float'>
<class 'int'>

Explanation: This code converts floating-point numbers to integers using the int() function, which removes the decimal part. It then prints the converted values and shows the data types of the original and converted numbers using type().

Using truncate function(trunc( ))

math( ) module is a standard built-in module in python. There are numerous mathematical functions defined in math( ) module. To use the truncate function, first, the math module must be imported, using trunc( ) function without defining the math( ) module gives an error. By using math.trunc( ) function a number can be truncated in python.

Python
import math

a = math.trunc(450.63)
print(a)
print(math.trunc(999998.99999))
print(math.trunc(-89.99))
print(math.trunc(0.99))

Output
450
999998
-89
0

Explanation:

  • math.trunc(450.63): Truncates 450.63 to 450.
  • math.trunc(999998.99999): Truncates 999998.99999 to 999998.
  • math.trunc(-89.99): Truncates -89.99 to -89 (truncation is not rounding, so it removes the decimal part).
  • math.trunc(0.99): Truncates 0.99 to 0.

Using split( ) function

The split( ) function only works on strings. Hence, the decimal values are converted to a string using str( ) function and then split at the decimal point. The values remain in the string data type after performing split( ) function. Therefore, the values are again converted to the integer data type.

Python
a = [998.99, 56.8, 25.6, -52.0]

# values are split at decimal point
l = []
for each in a:
    l.append(str(each).split('.')[0])

# all values converting to integer data type
b = [int(i) for i in l]
print(b)

Output
[998, 56, 25, -52]

Explanation:

  • split(‘.’): The code converts each number to a string and splits it at the decimal point, keeping only the integer part.
  • Conversion to int: The integer part is then converted back to an integer using int().

Using round()

round() function rounds the number to the nearest integer. By default, it rounds to the nearest integer and it rounds halfway cases (like 12.5) to the nearest even number.

Python
n = 12.675
r = round(n)
print(r) 

Output
13

Explanation: The round() function rounds 12.675 to the nearest integer. Since Python rounds halfway values to the nearest even number (also known as “bankers’ rounding”), 12.675 rounds to 12.



Next Article

Similar Reads