Pastes formatted x if numeric, otherwise x unmodified.
Circumvents R's automatic scientific notation.
If a number is nearly whole (see is_nearly_whole()), it is rounded to have
zero decimal places. Otherwise, numbers >= 1 are rounded to 1 decimal place;
numbers < 1 are rounded to have 2 significant digits.
Usage
format_number(x, nsmall = 1, signif_precise = 2)
Arguments
- x
inline code
- nsmall
number of digits after the decimal point to round to when x
is not nearly whole but x >= 1.
- signif_precise
number of significant digits to use when x is not
nearly whole
Value
formatted x if numeric, otherwise x unmodified.
Examples
format_number(0.0256)
#> [1] "0.026"
format_number(.Machine$double.eps^0.5)
#> [1] "0.000000015"
format_number(100000.08)
#> [1] "100,000.1"
format_number(1.00000000000000000001)
#> [1] "1"
format_number("this is a string")
#> [1] "this is a string"