formatNumber: Convert Number to Nicely Formatted Character String

Description Usage Arguments Value Examples

View source: R/utility_functions.R

Description

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.

Usage

1
formatNumber(x, decimals = 1, begin = "", end = "", pad_left = NULL)

Arguments

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.

Value

A character string, a formatted number.

Examples

 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 ) )

rettopnivek/utilityf documentation built on March 1, 2021, 7:05 p.m.