How to Remove Trailing Zeros from a Decimal in Python

A value like 2.500 reads better as 2.5. To remove trailing zeros from a decimal in Python, call normalize() on a Decimal and format the result with :f, so whole numbers don’t turn into scientific notation:

from decimal import Decimal
f"{Decimal('2.500').normalize():f}"    # '2.5'
f"{Decimal('500.000').normalize():f}"  # '500'

Pantry weights in pounds serve as the sample data here, and each script was run in Command Prompt with Python 3.12.5.

In this tutorial, we’ll cover Decimal.normalize(), the exponent trap, the :g format for floats, a string-based rstrip() approach, and one helper that handles every input type.

Remove trailing zeros from a Decimal with normalize()

normalize() strips the rightmost zeros from a Decimal. Whole numbers, though, come back as 5E+2 and 1.2E+2:

from decimal import Decimal

weights = ["2.500", "19.990", "0.750", "500.000", "120"]
for w in weights:
    d = Decimal(w)
    print(f"{w:>8} -> normalize(): {str(d.normalize()):<6} with :f -> {d.normalize():f}")

Output:

   2.500 -> normalize(): 2.5    with :f -> 2.5
  19.990 -> normalize(): 19.99  with :f -> 19.99
   0.750 -> normalize(): 0.75   with :f -> 0.75
 500.000 -> normalize(): 5E+2   with :f -> 500
     120 -> normalize(): 1.2E+2 with :f -> 120
Command Prompt output of Python Decimal normalize() removing trailing zeros, with 500.000 shown as 5E+2 until formatted with :f
Plain normalize() turns 500.000 into 5E+2; adding the :f format prints 500.

The value hasn’t changed, only its exponent. If you need a fixed number of places instead, see rounding numbers to 2 decimal places in Python.

How do you stop normalize() from returning 5E+2?

The decimal docs offer a small recipe that checks to_integral() and quantizes whole numbers to an integer instead of normalizing them:

from decimal import Decimal

def remove_exponent(d):
    # recipe from the decimal module FAQ
    return d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize()

for w in ["500.000", "120", "-4.50", "0.000"]:
    print(f"{w:>8} -> {remove_exponent(Decimal(w))}")

Output:

 500.000 -> 500
     120 -> 120
   -4.50 -> -4.5
   0.000 -> 0

I use this when the result must stay a Decimal for later math. For the opposite job, dropping the fraction entirely, read removing decimal numbers in Python.

Remove trailing zeros from a float with the :g format

A float never stores trailing zeros: 19.990 is already 19.99. The one zero you’ll see is in 2.0, and the g format spec drops it:

values = [3.50, 2.0, 19.990, 1500000.0, 0.0000125]
for x in values:
    print(f"{x!r:>12}  :g -> {x:<12g}  :.15g -> {x:.15g}")

Output:

         3.5  :g -> 3.5           :.15g -> 3.5
         2.0  :g -> 2             :.15g -> 2
       19.99  :g -> 19.99         :.15g -> 19.99
   1500000.0  :g -> 1.5e+06       :.15g -> 1500000
    1.25e-05  :g -> 1.25e-05      :.15g -> 1.25e-05
Command Prompt output of Python floats formatted with :g and :.15g, showing 1500000.0 switching to scientific notation with plain :g
Plain :g keeps six significant digits, so large values switch to e-notation; :.15g keeps them readable.

Use :.15g unless your values are tiny. Why floats print the way they do is covered in floating-point numbers in Python.

Strip trailing zeros from a number string with rstrip()

When the number arrives as text from a CSV or a form, rstrip("0").rstrip(".") works, but only after checking for a decimal point:

def strip_zeros(text):
    if "." not in text:          # "10" has no decimals to trim
        return text
    return text.rstrip("0").rstrip(".")

for s in ["12.5000", "8.000", "10", "0.0300", "250"]:
    print(f"{s:>8} -> {strip_zeros(s)}")

print("Trap 1:", "10".rstrip("0"))                 # zeros before the point
print("Trap 2:", format(0.0000125, "f"))           # 'f' keeps only 6 places

Output:

 12.5000 -> 12.5
   8.000 -> 8
      10 -> 10
  0.0300 -> 0.03
     250 -> 250
Trap 1: 1
Trap 2: 0.000013
Command Prompt output of a Python strip_zeros function trimming number strings, plus the 10 to 1 and format f rounding traps
The guarded function leaves 10 and 250 alone; without the check, "10".rstrip("0") returns 1.

Also, format(x, "f") keeps six places, so 0.0000125 becomes 0.000013. To parse the cleaned text, see converting a string to a float in Python.

A reusable Python helper for trailing zeros

My go-to version sends any input through Decimal(str(value)), then normalizes and formats with :f.:

from decimal import Decimal

def clean_number(value):
    """Return value as text with no trailing zeros and no exponent."""
    d = Decimal(str(value)).normalize()
    return f"{d:f}"

order = [("Flour", 2.500, "lb"), ("Rice", Decimal("10.00"), "lb"), ("Saffron", 0.0000125, "kg"),
         ("Eggs", 12, "count")]
for item, qty, unit in order:
    print(f"{item:<10} {clean_number(qty):>10} {unit}")

Output:

Flour             2.5 lb
Rice               10 lb
Saffron     0.0000125 kg
Eggs               12 count
Command Prompt output of a Python clean_number helper printing pantry quantities with trailing zeros removed and no scientific notation
Every quantity prints without trailing zeros, and the tiny saffron weight stays 0.0000125 instead of e-notation.

Don’t strip zeros from prices, though: $19.90 should keep two places. For money, follow printing 2 decimal places in Python instead.

Which method should you use?

InputMethodWatch out for
Decimalf"{d.normalize():f}"Plain normalize() gives 5E+2
floatf"{x:.15g}"Plain :g rounds to 6 digits
strrstrip("0").rstrip(".")Check for . first
Mixedclean_number()Returns text, not a number

You might also like these number guides:

Frequently asked questions

How do I remove trailing zeros from a decimal in Python?

Use f"{Decimal(value).normalize():f}". It strips the zeros and avoids scientific notation.

Why does normalize() return 5E+2?

For whole numbers it moves zeros into the exponent. Format with :f or quantize with Decimal(1).

Can I remove trailing zeros from a float?

Floats don’t store them. Use f"{x:.15g}" to print 2.0 as 2.

Why does rstrip(“0”) turn 10 into 1?

It strips zeros before the decimal point too. Only call it when the string contains a dot.

Does removing trailing zeros change the value?

No. 2.500 and 2.5 are equal; only the text you print changes.

Where is Decimal.normalize() documented?

See the decimal module docs, whose FAQ has the remove_exponent recipe.