signif2: round to fixed number of significant digits

View source: R/signif2.R

signif2R Documentation

round to fixed number of significant digits

Description

Even if theoretically correct, the built-in 'R' functions 'round' and 'signif' can be confusing (see examples). 'signif2' is a simple solution that can be used for reporting results consistently.

Usage

signif2(x, digits = 1, add, ...)

Arguments

x

a numeric vector.

digits

number of significant digits to round to. Must be an integer larger than 0.

add

pad with zeros where digits>nchar(x[i]). Currently not used.

...

additional arguments passed to formatC.

Value

A character vector.

Examples

x <- c(1.24e-4,1.1334e6,1.1,22.00000,10.00,1)
data.frame(x,s.3=signif(x,3),sc.3=as.character(signif(x,3)),s2.3=signif2(x,3))
signif2(c(.2,11.84),2)
## digits has no effect when x==0
signif2(0,1)
signif2(0,3)

NMcalc documentation built on Sept. 11, 2024, 7:51 p.m.