Description Usage Arguments Value Examples
View source: R/utility_functions.R
Provides several options to round, pad, and adjust a number to create a nicely formatted character string. Useful to ensuring matching character lengths to produce aligned values in a table.
1 | formatNumber(x, decimals = 1, begin = "", end = "", pad_left = NULL)
|
x |
A numeric value. |
decimals |
The number of decimal places to round to. |
begin |
An optional character string to add to the beginning of the output after formatting (e.g., '+', '~'). |
end |
An optional character string to add to the end of the output after formatting (e.g., '%'). |
pad_left |
Pads the output to a minimum number of characters by adding spaces to the left. |
A character string, a formatted number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # Default
print( formatNumber( 4.392 ) )
# Rounding
print( formatNumber( 4.392, decimals = 2 ) )
# Trailing zeros
print( formatNumber( 4, decimals = 1 ) )
print( formatNumber( 0.1, decimals = 2 ) )
# Adding units
print( formatNumber( 4.4, end = '%' ) )
# Adding leading symbols
print( formatNumber( 4.4, begin = '+' ) )
# Padding left side
print( formatNumber( 4.4, pad_left = 2 ) )
# Negative numbers
print( formatNumber( -.4 ) )
# Rounds to zero and pads appropriately
print( formatNumber( -.01 ) )
print( formatNumber( -.01, decimals = 0 ) )
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.