| pretty_num | R Documentation |
Uses as.numeric() to force a numeric value and then formats prettily
for easy presentation in console messages, reports, or dashboards.
This rounds to 0 decimal places by default, and adds in comma separators.
Expect that this will commonly be used for adding the pound symbol, the percentage symbol, or to have a +/- prefixed based on the value.
If applying over multiple or unpredictable values and you want to preserve
a non-numeric symbol such as "x" or "c" for data not available, use the
ignore_na = TRUE argument to return those values unaffected.
If you want to customise what NA values are returned as, use the alt_na
argument.
This function silences the warning around NAs being introduced by coercion.
pretty_num(
value,
prefix = "",
gbp = FALSE,
suffix = "",
dp = 0,
ignore_na = FALSE,
alt_na = FALSE,
nsmall = NULL,
dynamic_dp_value = NULL,
abbreviate = TRUE
)
value |
value to be prettified |
prefix |
prefix for the value, if "+/-" then it will automatically assign + or - based on the value |
gbp |
whether to add the pound symbol or not, defaults to not |
suffix |
suffix for the value, e.g. "%" |
dp |
number of decimal places to round to, 0 by default. |
ignore_na |
whether to skip function for strings that can't be converted and return original value |
alt_na |
alternative value to return in place of NA, e.g. "x" |
nsmall |
minimum number of digits to the right of the decimal point.
If NULL, the value of |
dynamic_dp_value |
Integer. Default = NULL.
Overrides the |
abbreviate |
whether to abbreviate large numbers to nearest million (where 1e6 <= value < 1e9) or billion (where value >= 1e9). |
string featuring prettified value
comma_sep() round_five_up() as.numeric()
Other prettying:
pretty_filesize(),
pretty_num_table(),
pretty_time(),
pretty_time_taken()
# On individual values
pretty_num(5789, gbp = TRUE)
pretty_num(564, prefix = "+/-")
pretty_num(567812343223, gbp = TRUE, prefix = "+/-")
pretty_num(11^9, gbp = TRUE, dp = 3)
pretty_num(-11^8, gbp = TRUE, dp = -1)
pretty_num(43.3, dp = 1, nsmall = 2)
pretty_num("56.089", suffix = "%")
pretty_num("x")
pretty_num("x", ignore_na = TRUE)
pretty_num("nope", alt_na = "x")
pretty_num(7.8e9, abbreviate = FALSE)
# dynamic_dp_value enabled for a billion value not divisible by 10
pretty_num(3e9, dynamic_dp_value = 2)
# dynamic_dp_value enabled for a billion value divisible by 10
pretty_num(10e9, dynamic_dp_value = 2)
# dynamic_dp_value enabled for a million value not divisible by 10
pretty_num(3e6, dynamic_dp_value = 3)
# dynamic_dp_value enabled for a million value divisible by 10
pretty_num(10e6, dynamic_dp_value = 3)
# dynamic_dp_value enabled with GBP and suffix
pretty_num(1.5e9,
gbp = TRUE, suffix = "%",
dynamic_dp_value = 1
)
#' # Applied over an example vector
vector <- c(3998098008, -123421421, "c", "x")
pretty_num(vector)
pretty_num(vector, prefix = "+/-", gbp = TRUE)
# Return original values if NA
pretty_num(vector, ignore_na = TRUE)
# Return alternative value in place of NA
pretty_num(vector, alt_na = "z")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.