Remove Trailing Zeros from Decimal in Python

I was working on a financial reporting project for a client in the USA, and I ran into a common formatting issue. The numbers in my report appeared to be 45.6000 or 120.000.

At first glance, this might not seem like a big deal. But when you’re preparing reports for stakeholders, clean and precise formatting matters a lot. Nobody wants to see unnecessary trailing zeros in a financial statement.

So, I started looking for ways to remove trailing zeros from decimal in Python. After testing multiple approaches, I found a few simple methods that work really well.

In this tutorial, I’ll show you four easy methods you can use to remove trailing zeros from a decimal in Python.

Method 1: Use a for-loop in Python

A for loop can also be very useful to remove the trailing zeros from a decimal in Python, as we can iterate over the string using it and can easily access the zeros.

temperature = "31.50"
inside = False
todays_temperature = ''
for x in reversed(temperature):
  if (not(inside) and x=="0"):
    pass
  else:
    todays_temperature = todays_temperature + x
    inside = True

temperature_update = todays_temperature[::-1]
print(temperature_update)

It will reverse the modified todays_temperature string to restore it to its original order and assign it to temperature_update.

temperature_update = todays_temperature[::-1]

Output:

31.5

You can refer to the screenshot below to see the output.

How to remove all the trailing zeros from a decimal using a for loop in Python

This method removes trailing zeros from a decimal by iterating through the string in reverse and reconstructing it without extra zeros.

Method 2: Use string slicing in Python

Python provides slicing of a string where we have to specify the start index and the end index, separated by a colon, to return a part of the string.

temperature = "31.500"
while (temperature[-1] == '0'):
  temperature = temperature[:-1]
print("Temperature after removing the trailing zero:",temperature)

I used this Python code to remove the trailing zeros from the given string.

 temperature = temperature[:-1]

Output:

Temperature after removing the trailing zero: 31.5

You can refer to the screenshot below to see the output.

Python program to remove trailing zeros from a decimal using string slicing

This method removes trailing zeros by repeatedly slicing off the last character until no zeros remain at the end of the string.

Method 3: Use the to_integral() & normalize() Function

Here, I will combine two functions from the decimal library to remove the trailing zeros in the given input.

The to_integral function is used to check whether the given input number has a fractional portion. In contrast, the normalize( ) function is used to remove the trailing zeros from a decimal in Python without any confusion.

from decimal import Decimal

input_num = Decimal('21.280000')
def remove_power(number):
    return (
        number.quantize(Decimal(1))
        if number ==number.to_integral()
        else number.normalize()
    )

print(remove_power(input_num))

I used the .normalize() method to remove all the trailing zeros after the decimal point.

  number.quantize(Decimal(1))
  if number ==number.to_integral()
  else number.normalize()

Output:

21.28

You can refer to the screenshot below to see the output.

Remove all trailing zeros from a decimal in Python

Here, the .quantize method is used to round the number to the nearest integer with no decimal places, and .to_integral() checks if the number is equal to its integral part.

Method 4: Use the str() & strip() function

The rstrip() method in Python operates on a given string and returns a string with trailing characters removed (and you can specify which trailing characters you are interested in).

This is another way to remove the trailing zeros from a decimal using the str() & rstrip() function in Python.

from decimal import Decimal

input_number = Decimal('1.489540000')
string = str(input_number)
output_number = string.rstrip('0')
print ("Truncated output number:", Decimal(output_number))
string = str(input_number)
output_number = string.rstrip('0')

Output:

Truncated output number: 1.48954

You can refer to the screenshot below to see the output.

How to remove the trailing zeros from a decimal in Python

We used str() to convert the variable to a string and then used the .rstrip(0), which stands for the right strip and removes the specified 0 argument from the end of the string.

Method 5: Use the float() function

Python provides this simple technique to remove trailing zeros from decimals using the float() function.

product_price = "2678.93000"
print("Product price:", product_price)
product_selling_price = float(product_price)
print("Updated selling price:", product_selling_price)

Output:

Product price: 2678.93000
Updated selling price: 2678.93

You can refer to the screenshot below to see the output.

Remove the trailing zeros from decimals in Python using the float() function

The float() function will convert the given input strictly into a float value without the space for trailing zeros.

Conclusion

I hope this article has provided a clear explanation of the different techniques that can be used to remove the trailing zeros from the float in Python.

I have covered these techniques with some illustrative examples, like using a for loop, string slicing, the to_integral() & normalize() function, the str() & rstrip() function, and the float() function to remove the trailing zeros from a decimal in Python.

You would choose the method to remove the trailing zeros for decimals in Python according to your requirements.

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.