signif.pad: Round Numbers to Specified Significant Digits with 0-Padding

Description Usage Arguments Value Author(s) See Also Examples

Description

A utility function to round numbers to a specified number of significant digits. Zeros are kept if they are significant.

Usage

1
signif.pad(x, digits = 3, round.integers = FALSE)

Arguments

x

A numeric vector.

digits

An interger specifying the number of significant digits to keep.

round.integers

Should rounding be limited to digits to the right of the decimal point?

Value

A character vector containing the rounded numbers.

Author(s)

Ben Rich <benjamin.rich@certara.com>

See Also

signif formatC prettyNum format

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
x <- c(0.9001, 12345, 1.2, 1., 0.1)
signif.pad(x, digits=3)
signif.pad(x, digits=3, round.integers=TRUE)

# Compare:
as.character(signif(x, digits=3))
format(x, digits=3, nsmall=3)
prettyNum(x, digits=3, drop0trailing=TRUE)
prettyNum(x, digits=3, drop0trailing=FALSE)

# This is very close.
formatC(x, format="fg", flag="#", digits=3) 
formatC(signif(x, 3), format="fg", flag="#", digits=3)

# Could always remove the trailing "."
sub("[.]$", "", formatC(x, format="fg", flag="#", digits=3))

benjaminrich/PCSmisc documentation built on May 24, 2019, 5:02 a.m.