Formatting of numbers, when they are to appear in messages. Displays only significant digits in a "nice way" and adds commas to separate thousands. It does much less than the format function, but also a bit more though.

fsignif(x, s = 2, r = 0, commas = TRUE)

signif_plus

Format

An object of class function of length 1.

Arguments

x

A numeric vector.

s

The number of significant digits to be displayed. Defaults to 2. All digits not in the decimal are always shown.

r

For large values, the number of digits after the decimals to be displayed (beyond the number of significant digits). Defaults to 0. It is useful to suggest that a number is not an integer.

commas

Whether or not to add commas to separate thousands. Defaults to TRUE.

Value

It returns a character vector of the same length as the input.

Examples


x = rnorm(1e5)
x[sample(1e5, 1e4, TRUE)] = NA

# Dumb function telling the number of NA values
tell_na = function(x) message("x contains ", fsignif(sum(is.na(x))), " NA values.")

tell_na(x)
#> x contains 9,473 NA values.

# Some differences with signif:
show_diff = function(x, d = 2) cat("signif(x, ", d, ") -> ", signif(x, d),
                                   " vs fsignif(x, ", d, ") -> ",
                                   fsignif(x, d), "\n", sep = "")

# Main difference is for large numbers
show_diff(95123.125)
#> signif(x, 2) -> 95000 vs fsignif(x, 2) -> 95,123
show_diff(95123.125, 7)
#> signif(x, 7) -> 95123.12 vs fsignif(x, 7) -> 95,123.12

# Identical for small numbers
show_diff(pi / 500)
#> signif(x, 2) -> 0.0063 vs fsignif(x, 2) -> 0.0063