Skip to contents

The two disagree on an exact half. Base R rounds a half to the even digit (round(0.5) is 0, round(2.5) is 2); SAS's ROUND() rounds it away from zero (0.5 is 1, 2.5 is 3, -0.5 is -1). Every formatter in the package – fmt_signif(), fmt_round(), fmt_numeric(), fmt_count_pct() – rounds with this same rule, so a study that has to match a SAS-produced table sets it once:


options(rtfreporter.rounding = "sas")

An explicit rounding = on any call still wins over the option.

"sas" also absorbs a binary representation error the way SAS's own fuzz does: 2.675 is stored as 2.67499999999999982, which base R rounds to 2.67 and SAS to 2.68.

These format numbers in a data frame or vector before a table is built; the fmt_* functions as_rtftables()'s cell_format takes (fmt_count_pct(), ...) format cells as the table is built.

Usage

round_num(x, digits = 0, rounding = NULL)

Arguments

x

A numeric vector.

digits

Decimal places. Default 0.

rounding

"r" (base R, half to even) or "sas" (half away from zero), in either case ("SAS" is "sas"; so for the option and every function's rounding). NULL, the default, reads getOption("rtfreporter.rounding"), which is "r" unless the session or site set it.

Value

A numeric vector the same length as x.

See also

fmt_round() for the same rounding as printed text.

Examples

round_num(c(0.5, 1.5, 2.5, -0.5))                    # 0 2 2 0
#> [1] 0 2 2 0
round_num(c(0.5, 1.5, 2.5, -0.5), rounding = "sas")  # 1 2 3 -1
#> [1]  1  2  3 -1
round_num(2.675, 2, rounding = "sas")                # 2.68
#> [1] 2.68