Skip to content

Round underlying floating point data by decimals digits

Source code

Description

Round underlying floating point data by decimals digits

Usage

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

Arguments

decimals Number of decimals to round by.
mode The rounding strategy used. A "rounded value" is a value with at most decimals decimal places (e.g. integers when decimals=0, multiples of 0.1 when decimals = 1, 0.01 when decimals = 2, and so on). Strategies that start with half\_ round all values to the nearest rounded value, only using the strategy to break ties when a value falls exactly between two rounded values (e.g. 0.5 when decimals = 0, 0.05 when decimals = 1). Other rounding strategies specify explicitly which rounded value is chosen and always apply (not just for tiebreaks).
  • “half_to_even” (default): Round to the nearest value; break ties by choosing the nearest even value. For example, 0.5 rounds to 0, 1.5 rounds to 2, 2.5 rounds to 2. Also known as "banker’s rounding"; this is the default because it tends to minimise cumulative rounding bias.
  • “half_away_from_zero”: Round to the nearest value; break ties by rounding away from zero. For example, 0.5 rounds to 1, -0.5 rounds to -1, 2.5 rounds to 3. Also known as "commercial rounding".
  • “to_zero”: Always round (truncate) towards zero, discarding the fractional part beyond decimals. For example, 0.9 rounds to 0, -0.9 rounds to 0, 1.29 rounds to 1.2 (with decimals = 1). Equivalent to $truncate().

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(a = c(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 = c(-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5),
  d = c("-3.5", "-2.5", "-1.5", "-0.5", "0.5", "1.5", "2.5", "3.5")
)$cast(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           │
#> └──────┴───────────────┴──────────┴───────────────┴─────────────┴───────────────┘