How to Remove the trailing Zeros from a Decimal in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
2 min

banner

# Remove the trailing zeros from a Decimal in Python

To remove trailing zeros from a decimal:

  1. Use the decimal.to_integral() method to check if the number has a fractional part.
  2. If it does, round the number and return it.
  3. If it does not, use the decimal.normalize() method to strip any trailing zeros.
main.py
from decimal import Decimal num = Decimal('1.230000') def remove_exponent(d): return ( d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize() ) print(remove_exponent(num)) # ๐Ÿ‘‰๏ธ 1.23

remove trailing zeros from decimal

The code for this article is available on GitHub

The first function is taken from the Decimal FAQ section of the official docs.

The to_integral() method rounds to the nearest integer.

main.py
from decimal import Decimal # ๐Ÿ‘‡๏ธ True print(Decimal('1.0000') == Decimal('1.0000').to_integral()) # ๐Ÿ‘‡๏ธ False print(Decimal('1.9000') == Decimal('1.9000').to_integral()) print(Decimal('1.0000').to_integral()) # ๐Ÿ‘‰๏ธ 1

If the number has no decimal part, we use the quantize() method to round to a fixed number of decimal places.

If the number has a decimal part, we use the Decimal.normalize() method to strip the rightmost trailing zeros.

main.py
from decimal import Decimal print(Decimal('1.230000').normalize()) # ๐Ÿ‘‰๏ธ 1.23 print(Decimal('3.456000000').normalize()) # ๐Ÿ‘‰๏ธ 3.456

We only use the normalize() method if the number has a decimal part because it could remove zeros to the left of the decimal.

main.py
from decimal import Decimal print(Decimal('500.000').normalize()) # ๐Ÿ‘‰๏ธ 5E+2 print(Decimal('510.100').normalize()) # ๐Ÿ‘‰๏ธ 510.1

Alternatively, you can use the str.rstrip() method.

# Remove trailing zeros from decimal using str.rstrip()

This is a two-step process:

  1. Use the str() class to convert the decimal to a string.
  2. Use the str.rstrip() method to strip the trailing zeros if the number has a decimal point.
main.py
from decimal import Decimal num = Decimal('1.230000') string = str(num) without_trailing_zeros = string.rstrip( '0').rstrip('.') if '.' in string else string result = Decimal(without_trailing_zeros) print(result) # ๐Ÿ‘‰๏ธ 1.23

remove trailing zeros from decimal using str rstrip

The code for this article is available on GitHub

The first step is to convert the decimal to a string.

The str.rstrip() method returns a copy of the string with the provided trailing characters removed.

We first strip trailing zeros, then try to strip the dot . if the decimal part only consisted of trailing zeros.

The example also checks whether the string contains a dot, so we don't attempt to strip trailing zeros from numbers that don't have a decimal part, e.g. 5000 to 5.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev