round_to: Round to any number

round_toR Documentation

Round to any number

Description

Round a numeric vector to any number, rounded up by default (round_up = TRUE).

Usage

round_to(x, to = 0.2, round_up = TRUE)

Arguments

x

Vector of numbers to round.

to

Number to round x to. A numeric scalar.

round_up

Whether to round a remainder of exactly to / 2 up or not. Set to FALSE in order to round off.

Details

This function's precision is limited to 15 significant digits in order to account for the limits of R's floating point representation.

A computationally more efficient alternative would be the unexported scales:::round_any() which drives scales::number() – with the drawback that it lacks control of rounding up exact remainders of accuracy / 2, i.e. it always rounds off.

Value

A numeric vector of the same length as x.

See Also

prettyunits::pretty_round()

Other statistical computing / numeric functions: safe_max(), safe_min(), safe_seq_len(), stat_mode()

Examples

vals = c(0.025, 0.1, 0.1999, 0.099999, 0.49, 0.55, 0.5, 0.9, 1)
vals |> pal::round_to(to = 0.05)
vals |> pal::round_to(to = 0.05,
                      round_up = FALSE)

salim-b/pal documentation built on Feb. 28, 2025, 6:51 p.m.