Remove Decimal Numbers in Python

When I started working with financial data in Python, I often needed to remove decimal numbers from values. For example, when processing sales reports from different states in the USA, I had to clean up amounts like 1234.56 into 1234.

At first, I thought there was just one way to do it. But with more than 10 years of experience as a Python developer, I’ve learned there are multiple ways to remove decimals in Python, each useful in different scenarios.

In this tutorial, I’ll show you five easy methods I personally use to remove decimal numbers in Python. I’ll also explain when to use each method, so you can pick the one that best fits your project.

Method 1: Use Python’s int() Function

Python provides a built-in function int() to typecast a value to an integer and remove the decimal portion of the number.

marks_obtained = 90.44
cut_off = 140
negative_marking = -1.5

print(int(marks_obtained))
print(int(cut_off))
print(int(negative_marking))

print(type(marks_obtained))
print(type(cut_off))
print(type(negative_marking))

Output:

90
140
-1
<class 'float'>
<class 'int'>
<class 'float'>

You can see the output in the screenshot below.

Remove decimal numbers using int() function in Python

To remove decimal places in Python, we will pass a float value with a decimal part to this function, which will remove decimal places in Python.

Method 2: Use math.trunc() Method

We can remove decimal places in Python using the trunc() function from the math library. This function takes a float value and removes the decimal part from the same, returning the integer value.

import math

num_one = 1.53
num_two = -6.12
num_three = 100.234

print(math.trunc(num_one))
print(math.trunc(num_two))
print(math.trunc(num_three))

Output:

1
-6
100

You can see the output in the screenshot below.

Python program to remove decimal numbers using the math module with trunc() function

This method will not round the number up/down to the nearest integer but remove the decimal.

Method 3: Use math.ceil() Method in Python

Python provides the math module to perform mathematical tasks.

import math


Chicago_latitude = 41.881832
Chicago_longitude =  -87.623177

print(math.ceil(Chicago_longitude))

print(math.ceil(Chicago_latitude))

Output:

42
-87

You can see the output in the screenshot below.

Python program to remove decimal numbers using the math.ceil method

The ceil() function in Python is used to round a number up to the nearest integer, if necessary, and returns the result.

Method 4: Use the round() Function

The built-in round() function provided by Python is capable of rounding off the float value to the nearest integer value.

input_num1 = 1.53
print(round(input_num1))

input_num2 = -11.15
print(round(input_num2))

input_num3 = 128.2341
print(round(input_num3))

Output:

2
-11
128

You can see the output in the screenshot below.

How to remove decimal number using the round() function in Python

If the decimal places to be rounded are not specified, it is considered 0.

Method 5: Use String Formatting and Methods

In Python, there are two ways of using string formatting and methods to remove decimal numbers in Python. Everything returned in this section will be a string.

  • Using split() function
  • Using % placeholder

1. Use split() function

The split() function in Python only works on strings after breaking the given string by the specified separator.

decimal_value = [98.19, 56.8, 25.6, -52.0] 
empty_list = []

for each in decimal_value: 
    empty_list.append(str(each).split('.')[0]) 
  
final_list = [int(i) for i in empty_list] 
print(final_list)

It iterates over each element in a list or iterable called decimal_value. For each element in decimal_value, it converts it to a string (str(each)).

empty_list.append(str(each).split('.')[0]) 

Output:

You can see the output in the screenshot below.

How to remove decimal number using the round() function in Python

Splits it at the decimal point (‘.’), then appends the part before the decimal point to a list called empty_list.

2. Use %d placeholder

This is another way to remove decimal numbers in Python using the %d placeholder.

temperature = 29.8
print('%d'%temperature)
print(type(temperature))

Output:

29
<class 'float'>

You can see the output in the screenshot below.

Python program to remove decimal numbers using the %d placeholder

This is used to round off the given numbers, and the numbers after the decimal are excluded. It will only extract the integer parts.

Conclusion

This tutorial provides information about how to remove decimal numbers in Python using different methods, like using the int() function, math.trunc() method, math.ceil() method, round() function, string formatting method (using split() function and using %d placeholder), split() function, and %d placeholder with examples.

I hope you understand all of them.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.