How to remove the trailing zeros from a decimal in Python [4 Methods]

In this Python tutorial, I will give examples of how to write a Python program to remove the trailing zeros from a decimal using different methods.

For example, you might have a number such as “314.60” or “12.4000,” and you desire to remove the trailing zeros so that it returns as “314.56” or “123.4”.

There are effective methods to remove the trailing zeros from a decimal in Python: using for loop, string slicing, the to_integral() & normalize() function, the str() function and rstrip() function, and the float() functions.

Python program to remove the trailing zeros from a decimal using a for-loop

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.

This is the full source code of Python to remove the trailing zeros from a decimal using a for-loop.

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 see in the screenshot below the Python program output to remove all the trailing zeros from a decimal using a for loop.

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

How to remove the trailing zeros from a decimal using string slicing in Python

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

READ:  Python print 2 decimal places [%.2f in Python]

Below is the Python code to remove the trailing zeros from a decimal using string slicing.

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

The screenshot below shows the Python program’s output to remove the trailing zeros from a decimal using string slicing.

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

How to remove the trailing zeros from a decimal in Python using 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.

Here is the full Python code to remove the trailing zeros from a decimal in Python using the to_integral() & normalize() function.

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))

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.

Then, 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

Refer to the image below to see the output of the Python program to remove trailing zeros from a decimal using the to_integral() & normalize() function in Python.

Remove all trailing zeros from a decimal in Python

Remove the trailing zeros from decimal in Python using 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).

READ:  How to Perform String Slicing in Python

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

Here is the complete Python source code to remove the trailing zeros from a decimal using the str() & rstrip() functions.

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))

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.

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 of how to remove trailing zeros from a decimal using the str() & rstrip() functions in Python.

How to remove the trailing zeros from a decimal in Python

Python program to remove trailing zeros from decimal using the float() function

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

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

This is the Python source code to remove the 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

The screenshot below shows how to remove trailing zeros from decimals in Python using the float() function.

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

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_intergral() & normalize() function, the str() & rstrip() function, and the float() function to remove the trailing zeros from a decimal in Python.

READ:  How to Swap Two Numbers in Python Using Function [4 Examples]

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

You may also like to read: