This function is used within format_value to prepare numeric values within
cells for formatting and display.
Arguments
- x
numeric(1). Value to format
- digits
numeric(1). Number of digits to round to, or
NAto convert to a character value with no rounding.- na_str
character(1). The value to return if
xisNA.
Value
A character value representing the value after rounding, containing containing any trailling zeros required to display exactly
digits
elements.
Details
This function combines the rounding behavior of R's standards-complaint
round function (see the Details section of that documentation)
with the strict decimal display of sprintf. The exact behavior
is as follows:
If
xis NA, the value ofna_stris returnedIf
xis non-NA butdigitsis NA,xis converted to a character and returnedIf
xanddigitsare both non-NA,roundis called first, and thensprintfis used to convert the rounded value to a character with the appropriate number of trailing zeros enforced.
Note
This differs from the base R round function in that NA
digits indicate x should be passed 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).