HOWTO · Python
How to Truncate Float in Python
This tutorial explains how to truncate a float in Python.
On this page
A Python float cannot be shortened in place: it is a binary floating-point value, not a decimal string with a fixed number of digits. To discard digits after a chosen decimal place, create a Decimal from the number’s decimal text and quantize it with ROUND_DOWN. For an integer result, use math.trunc() or int(). These operations truncate toward zero; round() and numeric formatting have different behavior.
Choose truncation or rounding
Truncation discards digits beyond a chosen position and moves toward zero. Rounding chooses a nearby value according to a rounding rule. Python’s round(number, ndigits) rounds; it does not truncate. For example, round(3.146, 2) rounds to 3.15, while truncating to two decimal places gives 3.14. Python’s built-in round() uses ties-to-even for halfway cases, so it should not be treated as a general decimal truncation method.
Truncate a float to a fixed number of decimal places
Use Decimal when the task is to keep a specified number of decimal places. Build it from a string representation, then call quantize() with ROUND_DOWN. In the decimal module, ROUND_DOWN rounds toward zero: it discards digits toward zero, so negative values move toward zero rather than toward a more negative value. Passing a string such as "3.14159" preserves the intended decimal input; constructing Decimal directly from a float instead captures that float’s binary approximation.
Run this example:
from decimal import Decimal, ROUND_DOWN
values = ("3.14159", "-3.14159", "0", "2.1")
places = 2
quantum = Decimal("1").scaleb(-places)
for text in values:
print(Decimal(text).quantize(quantum, rounding=ROUND_DOWN))
3.14
-3.14
0.00
2.10
The first two results show truncation toward zero for both signs, including the negative input. Quantization also keeps the requested number of fractional places: zero becomes 0.00, and 2.1 becomes 2.10. If you already have a float and want its shortest displayed decimal representation as input, use Decimal(str(value)); that cannot recover decimal information that was lost before the float was created. For money or other exact decimal quantities, prefer accepting decimal text or constructing Decimal from integers and strings from the start. See the decimal module documentation.
Truncate toward zero to an integer
For an integer rather than a fixed number of decimal places, math.trunc() explicitly returns the integer part toward zero. The built-in int() does the same when converting a float. For example, math.trunc(5.67) is 5, and math.trunc(-3.45) is -3. Neither retains decimal places; use Decimal.quantize() when the result needs a fixed decimal scale. math.trunc() also accepts numeric types that implement __trunc__(), which supplies the integer value used for truncation.
Why scaling a float can give unexpected results
A common shortcut multiplies a float by a power of ten, converts the result to an integer, and divides again. This can work for some values, but binary floats cannot exactly represent many decimal fractions. The multiplication may therefore land just below or above the decimal value you intended, so int(value * 100) / 100 can discard an unexpected digit. It also returns another float, which still has binary representation limits. Avoid relying on this pattern for decimal-sensitive work; use Decimal with text input instead. For calculations whose domain is inherently approximate, keep the values as floats and account for their precision rather than treating their printed digits as exact.
Choose the right method
- Use
Decimalcreated from decimal text andquantize(..., rounding=ROUND_DOWN)to truncate to a fixed number of decimal places. - Use
math.trunc()orint()to discard the fractional part and return an integer toward zero. - Use
round()when the desired operation is rounding, not truncation. - Use formatting when you only need to control displayed text. An f-string such as
f"{value:.2f}"produces a string and rounds its displayed value; it does not change or truncate the underlying float.
Choose based on the result you need: a decimal value at a stated precision, an integer, a rounded value, or only a formatted string.