How to remove all decimals from a number using Python?
Last Updated :
27 Mar, 2025
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))
OutputNumber1 = 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))
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)
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)
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.
Similar Reads
How to format numbers as currency strings in Python Formatting numbers as currency strings in Python is a common requirement especially in the applications involving financial data. Formatting numbers as currency strings in Python involves presenting numeric values in the standardized currency format typically including the currency symbol, thousands
3 min read
Remove Seconds from the Datetime in Python In Python, the datetime module provides classes for manipulating dates and times. Sometimes we may need to remove the seconds component from a datetime object, either for display purposes, to store in a database, or for formatting consistency. In this article we will explore different approaches to
2 min read
How to Divide Decimals by Whole Numbers Dividing decimals by whole numbers is a fundamental math skill that plays a crucial role in daily life, from handling money to measuring ingredients. Understanding how to divide decimals by whole numbers accurately ensures you can solve a variety of mathematical problems with ease. In this article,
5 min read
How to Add leading Zeros to a Number in Python In this article, we will learn how to pad or add leading zeroes to the output in Python. Example:Input: 11 Output: 000011 Explanation: Added four zeros before 11(eleven).Display a Number With Leading Zeros in PythonAdd leading Zeros to numbers using format() For more effective handling of sophistica
3 min read
How to convert a whole number into a decimal? Answer: To convert a whole number into a decimal, simply add a decimal point and zero after the number. For Example:Given that the whole number is 5.Add a decimal and a zeros to the right side of the whole number.5 = 5.0Hence, the decimal number is 5.0Also Read : Convert Decimal To Mixed NumberHow t
5 min read