Skip to contents

Rounds and formats numeric vectors for display. Rounding is half away from zero (0.5 becomes 1), the big mark is inserted for absolute values of 1,000 or more, and missing values are returned as na. The defaults for big.mark and decimal.mark can be set globally with options(ltsummary.big.mark = , ltsummary.decimal.mark = ); when the decimal mark is a comma the default big mark is a thin space.

Usage

style_number(
  x,
  digits = 0,
  big.mark = ifelse(decimal.mark == ",", " ", ","),
  decimal.mark = getOption("OutDec"),
  scale = 1,
  prefix = "",
  suffix = "",
  na = NA_character_,
  ...
)

Arguments

x

(numeric)
Numeric vector.

digits

(integer)
Number of decimal places. Recycled to the length of x.

big.mark

(string)
Character used between every three digits to the left of the decimal mark.

decimal.mark

(string)
Character used as the decimal mark.

scale

(scalar numeric)
Values are multiplied by scale before rounding, e.g. scale = 100 to show a proportion as a percentage.

prefix, suffix

(string)
Text added before and after each formatted value.

na

(string)
Replacement for missing values.

...

Not used. Accepted so that formatting functions can be called interchangeably.

Value

A character vector the same length as x. Names and dimensions of x are preserved.

Examples

c(0.111, 12.3, 1234.5678, -0.5, NA) |> style_number(digits = 1)
#> [1] "0.1"     "12.3"    "1,234.6" "-0.5"    NA       
c(0.111, 12.3) |> style_number(digits = c(1, 0))
#> [1] "0.1" "12" 

# half away from zero, unlike round()
style_number(c(0.5, 1.5, 2.5))
#> [1] "1" "2" "3"
round(c(0.5, 1.5, 2.5))
#> [1] 0 2 2

style_number(0.123, digits = 1, scale = 100, suffix = "%")
#> [1] "12.3%"
style_number(1234.5, decimal.mark = ",")
#> [1] "1 235"