Skip to content

Round underlying floating point data by decimals digits

Description

Round underlying floating point data by decimals digits

Usage

<Expr>$round(decimals, mode = c("half_to_even", "half_away_from_zero"))

Arguments

decimals Number of decimals to round by.
mode Rounding mode. One of “half_to_even” (default) or “half_away_from_zero”.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(a = c(0.5, 1.5, 2.5, 3.5))

df$with_columns(
  half_to_even = pl$col("a")$round(0),
  half_away_from_zero = pl$col("a")$round(0, "half_away_from_zero"),
)
#> shape: (4, 3)
#> ┌─────┬──────────────┬─────────────────────┐
#> │ a   ┆ half_to_even ┆ half_away_from_zero │
#> │ --- ┆ ---          ┆ ---                 │
#> │ f64 ┆ f64          ┆ f64                 │
#> ╞═════╪══════════════╪═════════════════════╡
#> │ 0.5 ┆ 0.0          ┆ 1.0                 │
#> │ 1.5 ┆ 2.0          ┆ 2.0                 │
#> │ 2.5 ┆ 2.0          ┆ 3.0                 │
#> │ 3.5 ┆ 4.0          ┆ 4.0                 │
#> └─────┴──────────────┴─────────────────────┘