# Both `round_up()` and `round_down()` work like
# `base::round()` unless the closest digit to be
# cut off by rounding is 5:
round_up(x = 9.273, digits = 1) # 7 cut off
round_down(x = 9.273, digits = 1) # 7 cut off
base::round(x = 9.273, digits = 1) # 7 cut off
round_up(x = 7.584, digits = 2) # 4 cut off
round_down(x = 7.584, digits = 2) # 4 cut off
base::round(x = 7.584, digits = 2) # 4 cut off
# Here is the borderline case of 5 rounded by
# `round_up()`, `round_down()`, and `base::round()`:
original <- c( # Define example values
0.05, 0.15, 0.25, 0.35, 0.45,
0.55, 0.65, 0.75, 0.85, 0.95
)
tibble::tibble( # Output table
original,
round_up = round_up(x = original, digits = 1),
round_down = round_down(x = original, digits = 1),
base_round = base::round(x = original, digits = 1)
)
# (Note: Defining `original` as `seq(0.05:0.95, by = 0.1)`
# would lead to wrong results unless `original` is rounded
# to 2 or so digits before it's rounded to 1.)
Run the code above in your browser using DataLab