round_fmt | R Documentation |
This function is used within format_value()
to prepare numeric values within
cells for formatting and display.
round_fmt(x, digits, na_str = "NA")
x |
( |
digits |
( |
na_str |
( |
This function combines the rounding behavior of R's standards-compliant round()
function (see the Details section of that documentation) with the strict decimal display
of sprintf()
. The exact behavior is as follows:
If x
is NA
, the value of na_str
is returned.
If x
is non-NA
but digits
is NA
, x
is converted to a character and returned.
If x
and digits
are both non-NA, round()
is called first, and then sprintf()
is used to convert the rounded value to a character with the appropriate number of trailing
zeros enforced.
A character value representing the value after rounding, containing any trailing zeros
required to display exactly digits
elements.
This differs from the base R round()
function in that NA
digits indicate x
should be converted
to character and returned unchanged whereas round(x, digits=NA)
returns NA
for all values of x
.
This behavior will differ from as.character(round(x, digits = digits))
in the case where there are
not at least digits
significant digits after the decimal that remain after rounding. It may differ from
sprintf("\%.Nf", x)
for values ending in 5
after the decimal place on many popular operating systems
due to round
's stricter adherence to the IEC 60559 standard, particularly for R versions > 4.0.0 (see
warning in round()
documentation).
format_value()
, round()
, sprintf()
round_fmt(0, digits = 3)
round_fmt(.395, digits = 2)
round_fmt(NA, digits = 1)
round_fmt(NA, digits = 1, na_str = "-")
round_fmt(2.765923, digits = NA)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.