round_half_up: Set rules for rounding ties

View source: R/round_spec.R

round_half_upR Documentation

Set rules for rounding ties

Description

Rounding a number x to the nearest integer requires some tie-breaking rule for those cases when x is exactly half-way between two integers, that is, when the fraction part of x is exactly 0.5. The round_half_up() function implements a tie-breaking rule that consistently rounds half units upward. Although this creates a slight bias toward larger rounded outputs, it is widely used in many disciplines. The round_half_even() function breaks ties by rounding to the nearest even unit.

Usage

round_half_up(rspec)

round_half_even(rspec)

Arguments

rspec

a rounding_specification object (see round_spec).

Value

an object of class rounding_specification.

See Also

Other rounding helpers: round_using_magnitude()

Examples


# note base R behavior rounds to even:
round(0.5) # --> 0
round(1.5) # --> 2
round(2.5) # --> 2

# make rspec that rounds up
rspec <- round_half_up(round_spec())
rspec <- round_using_decimal(rspec, digits = 0)

# check
table_value(0.5, rspec) # --> 1
table_value(1.5, rspec) # --> 2
table_value(2.5, rspec) # --> 3

# make rspec that rounds even
rspec <- round_half_even(round_spec())
rspec <- round_using_decimal(rspec, digits = 0)

# check
table_value(0.5, rspec) # --> 0
table_value(1.5, rspec) # --> 2
table_value(2.5, rspec) # --> 2

table.glue documentation built on Feb. 16, 2023, 9:29 p.m.