Control decimal precision
Normalize measured or monetary-like floating-point columns before display or downstream calculations.
Polars · Module
Rounds floating-point expression values to a specified number of decimal places. The default half_to_even mode resolves ties by choosing the nearest even value.
Expr.round returns an expression that rounds floating-point values to a chosen number of decimal places. It uses the selected strategy for tie handling or, with to_zero, truncates toward zero.
Expr.round(decimals: int = 0, mode: RoundMode = 'half_to_even') → Expr| Name | Type | Description |
|---|---|---|
decimals | int = 0 | Number of decimals to round by. |
mode | RoundMode = 'half_to_even' | Rounding strategy; one of half_to_even, half_away_from_zero, or to_zero. |
Expr
import polars as pl
df = pl.DataFrame({"price": [1.234, 2.345, 2.5]})
result = df.select(pl.col("price").round(2))
print(result)Rounds the selected floating-point column to two decimal places using the default half-to-even strategy.
Normalize measured or monetary-like floating-point columns before display or downstream calculations.
Apply predictable tie-breaking when aggregating or presenting numeric results.
Remove fractional precision without nearest-value rounding.
It rounds the underlying floating-point data by the requested number of decimal digits and returns an Expr. With decimals=0, values are rounded to whole-number precision.
Follow-up: What happens for an exact tie under the default mode? The nearest even rounded value is selected, such as 0.5 to 0 and 1.5 to 2.
half_to_even rounds to the nearest value and resolves ties using the nearest even value. This is the default strategy and is also called banker’s rounding.
Follow-up: Why use it by default? The documentation says it tends to minimize cumulative rounding bias.
The to_zero mode always truncates toward zero, discarding fractional digits beyond the requested precision. Unlike half-based modes, it is not only a tie-breaking rule; it always determines the chosen rounded value.
Follow-up: Which related operation has equivalent behavior? The documentation states that Expr.round with to_zero is equivalent to truncate().
polars.Series.roundpolars.Expr.truncatepolars.Expr.dt.round