signif.pad | R Documentation |
A utility function to round numbers to a specified number of significant digits. Zeros are kept if they are significant.
signif.pad(x, digits = 3, round.integers = FALSE)
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? |
A character vector containing the rounded numbers.
Benjamin Rich <mail@benjaminrich.net>
signif
formatC
prettyNum
format
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))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.