Remove trailing zeros from decimal in Python

avatar

Borislav Hadzhiev

Last updated: Jul 10, 2022

banner

Photo from Unsplash

Remove trailing zeros from 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') # ✅ drop trailing zeros from decimal, using normalize() def remove_exponent(d): return d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize() print(remove_exponent(num)) # 👉️ 1.23 # -------------------------------- # ✅ drop trailing zeros from decimal, using str.rstrip() num_2 = Decimal('1.230000') string = str(num_2) without_trailing_zeros = string.rstrip( '0').rstrip('.') if '.' in string else string result = Decimal(without_trailing_zeros) print(result) # 👉️ 1.23

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.

To remove trailing zeros from a decimal:

  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

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.

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.