Remove Decimals from a String in Python

Recently, I was working on a project where I had to clean up some messy financial data. The data came from different U.S. retail systems, and many of the values had decimals attached to them inside strings.

For example, I had strings like “Product123.45” or “Invoice-567.89”. What I really needed was just the integer part or the text without the decimals.

The challenge? Python’s built-in function doesn’t directly remove decimals from a string. So, I had to try a few different approaches until I found what worked best.

In this tutorial, I’ll share the methods I use most often. I’ll explain them step by step, so you can follow along easily and apply them to your own projects.

Method 1: Use the re.sub() Method in Python

In Python, the sub() is a function in the built-in re module that handles regular expressions. The sub() function in Python returns a string after replacing the matched pattern in a string with a replacement.

import re

def remove_decimal(string_with_decimal):
    return re.sub(r'\.\d+', '', string_with_decimal)

string_with_decimal = "412.66"
result = remove_decimal(string_with_decimal)
print(result)

This Python code line is used to find all occurrences of a decimal followed by one or more digits and replace them with an empty string, then remove the decimal part from the string.

 return re.sub(r'\.\d+', '', string_with_decimal)

Output:

412

I executed the above example code and added the screenshot below.

How to remove decimals from a string in Python

We can use regular expressions to remove decimal parts in Python from strings by matching the pattern of numbers with decimals and extracting the integer part.

Method 2: Use Python’s split() and join() Method

This is another approach to remove decimals from a string using the split() and join() methods in Python.

def remove_decimal_num(string_decimal):
    return ''.join(string_decimal.split('.'))

string_decimal = "672.56"
output_num = remove_decimal_num(string_decimal)
print(output_num)

We used this to split the input string at each occurrence of the decimal point and create a list of substrings.

Then, the join() function in Python keeps the substrings back together into a single string, and an empty string is used as a separator to join the substrings together and remove the decimal point.

 return ''.join(string_decimal.split('.'))

Output:

67256

I executed the above example code and added the screenshot below.

Python program to remove decimals from a string

The split() function in Python is used to split a string into a list, whereas the join() function takes all items in an iterable and joins them into one string.

Method 3: Use the math.floor() Method

In Python, the math.floor() method rounds a number down to the nearest integer and returns the result.

import math

def remove_number(string_with_decimal):
    return str(math.floor(float(string_with_decimal)))

string_with_decimal = "37.56"
output_value = remove_number(string_with_decimal)
print(output_value) 

Here, it rounds down the given number to the nearest integer, and then it converts the integer back to a string.

 return str(math.floor(float(string_with_decimal)))

Output:

37

I executed the above example code and added the screenshot below.

How to remove decimals from a string in Python using math.floor() method

This method removes the decimal part by rounding the number down to the nearest integer and converting it back to a string.

Method 4: Use a custom function with string conversion

This is another approach to removing decimals from a string using a custom function with string conversion in Python.

def remove_decimal(string_with_decimal):
    return str(int(float(string_with_decimal)))

string_with_decimal = "3711.56"
result = remove_decimal(string_with_decimal)
print(result)

Here, I will convert the float value of string_with_decimal into an integer to remove the decimals. Then, convert that value into a string using the str() function.

return str(int(float(string_with_decimal)))

Output:

3711

I executed the above example code and added the screenshot below.

Python program to remove decimals from a string using string conversion

This method removes the decimal part by converting the string to a float, casting it to an integer, and then converting it back to a string.

I hope the information in the above Python post helped you understand how to remove decimals from a string in Python.

Here, I have demonstrated several methods with some illustrative examples to remove decimals from a string in Python, such as the re.sub() method, split(), and join() methods, and math.floor() method, and string conversion.

You may also like to read the following articles related to Python:

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.