Skip to contents

2 Decimal places, otherwise scientific notation

Usage

ddSci(x, decimal_places = 2, hi = 1e+06, as_numeric = FALSE)

Arguments

x

Vector of numbers

decimal_places

Integer: Return this many decimal places.

hi

Float: Threshold at or above which scientific notation is used.

as_numeric

Logical: If TRUE, convert to numeric before returning. This will not force all numbers to print 2 decimal places. For example: 1.2035 becomes "1.20" if as_numeric = FALSE, but 1.2 otherwise This can be helpful if you want to be able to use the output as numbers / not just for printing.

Value

Formatted number

Details

Numbers will be formatted to 2 decimal places, unless this results in 0.00 (e.g. if input was .0032), in which case they will be converted to scientific notation with 2 significant figures. ddSci will return 0.00 if the input is exactly zero. This function can be used to format numbers in plots, on the console, in logs, etc.

Author

EDG

Examples

if (FALSE) { # \dontrun{
x <- .34876549
ddSci(x)
# "0.35"
x <- .00000000457823
ddSci(x)
# "4.6e-09"
} # }