polars.Expr.round#

Expr.round(decimals: int = 0, mode: RoundMode = 'half_to_even') Expr[source]#

Round underlying floating point data by decimals digits.

The default rounding mode is “half to even” (also known as “bankers’ rounding”).

Parameters:
decimals

Number of decimals to round by.

mode{‘half_to_even’, ‘half_away_from_zero’}

RoundMode.

  • half_to_even

    round to the nearest even number

  • half_away_from_zero

    round to the nearest number away from zero

Examples

>>> df = pl.DataFrame({"a": [0.33, 0.52, 1.02, 1.17]})
>>> df.select(pl.col("a").round(1))
shape: (4, 1)
┌─────┐
│ a   │
│ --- │
│ f64 │
╞═════╡
│ 0.3 │
│ 0.5 │
│ 1.0 │
│ 1.2 │
└─────┘
>>> df = pl.DataFrame(
...     {
...         "f64": [-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5],
...         "d": ["-3.5", "-2.5", "-1.5", "-0.5", "0.5", "1.5", "2.5", "3.5"],
...     },
...     schema_overrides={"d": pl.Decimal(scale=1)},
... )
>>> df.with_columns(
...     pl.all().round(mode="half_away_from_zero").name.suffix("_away"),
...     pl.all().round(mode="half_to_even").name.suffix("_to_even"),
... )
shape: (8, 6)
┌──────┬───────────────┬──────────┬───────────────┬─────────────┬───────────────┐
│ f64  ┆ d             ┆ f64_away ┆ d_away        ┆ f64_to_even ┆ d_to_even     │
│ ---  ┆ ---           ┆ ---      ┆ ---           ┆ ---         ┆ ---           │
│ f64  ┆ decimal[38,1] ┆ f64      ┆ decimal[38,1] ┆ f64         ┆ decimal[38,1] │
╞══════╪═══════════════╪══════════╪═══════════════╪═════════════╪═══════════════╡
│ -3.5 ┆ -3.5          ┆ -4.0     ┆ -4.0          ┆ -4.0        ┆ -4.0          │
│ -2.5 ┆ -2.5          ┆ -3.0     ┆ -3.0          ┆ -2.0        ┆ -2.0          │
│ -1.5 ┆ -1.5          ┆ -2.0     ┆ -2.0          ┆ -2.0        ┆ -2.0          │
│ -0.5 ┆ -0.5          ┆ -1.0     ┆ -1.0          ┆ -0.0        ┆ 0.0           │
│ 0.5  ┆ 0.5           ┆ 1.0      ┆ 1.0           ┆ 0.0         ┆ 0.0           │
│ 1.5  ┆ 1.5           ┆ 2.0      ┆ 2.0           ┆ 2.0         ┆ 2.0           │
│ 2.5  ┆ 2.5           ┆ 3.0      ┆ 3.0           ┆ 2.0         ┆ 2.0           │
│ 3.5  ┆ 3.5           ┆ 4.0      ┆ 4.0           ┆ 4.0         ┆ 4.0           │
└──────┴───────────────┴──────────┴───────────────┴─────────────┴───────────────┘