Skip to contents

Rounds to digits decimal places and prints them all, trailing zeros included – fmt_round(2.5, 2) is "2.50", not "2.5".

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

fmt_round(x, digits = 2L, rounding = NULL, na = "")

Arguments

x

A numeric vector. Character input is an error – character columns are taken as already formatted.

digits

Decimal places. Default 2.

rounding

"r" rounds with base::round(), which is banker's rounding; "sas" rounds half away from zero as SAS does. They differ on exact halves – 23.445 is "23.44" under "r" and "23.45" under "sas". NULL, the default, reads getOption("rtfreporter.rounding") ("r" unless set); see round_num().

na

Text for NA and NaN. Default "" (an empty cell, which is how rtfreporter renders a missing value anyway).

Value

A character vector the same length as x. Inf / -Inf pass through as "Inf" / "-Inf".

Examples

fmt_round(c(2.5, 20.333, 100), digits = 2)
#> [1] "2.50"   "20.33"  "100.00"
fmt_round(23.445, digits = 2)                   # "23.44" -- banker's
#> [1] "23.44"
fmt_round(23.445, digits = 2, rounding = "sas") # "23.45"
#> [1] "23.45"