Skip to content

Commit

Permalink
Create custom rounding function for fmt_fraction()
Browse files Browse the repository at this point in the history
  • Loading branch information
rich-iannone committed May 5, 2021
1 parent efcc0c2 commit a0aa613
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions R/format_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ fmt_fraction <- function(data,

# Round all values of x to the highest
# granularity required for this formatter
x <- round(x, 3)
x <- round_gt(x, 3)

# Divide the `x` values in 'big' and 'small' components
big_x <- trunc(x)
Expand Down Expand Up @@ -827,7 +827,7 @@ get_frac_tbl <- function(accuracy, values) {
stop("Unknown `accuracy` value.")
)

values <- round(values, digits = digits)
values <- round_gt(values, digits = digits)

lookup_fractions(lu_tbl = lu_tbl, values = values)
}
Expand Down Expand Up @@ -875,7 +875,7 @@ lookup_fractions <- function(lu_tbl, values) {

} else {

idx <- round(value, precision) == round(lu_tbl$value, precision)
idx <- round_gt(value, precision) == round_gt(lu_tbl$value, precision)

if (!has_rounding) {

Expand Down Expand Up @@ -944,6 +944,19 @@ generate_fractions <- function(frac_tbl, layout, context) {
fractions
}

# The `round_gt()` function is used in gt over `base::round()` for consistency
# in rounding across R versions; it uses the 'Round-Half-Up' (R-H-U) algorithm,
# which is *not* used in R >= 4.0
round_gt <- function(x, digits = 0) {

x_sign <- sign(x)
z <- abs(x) * 10^digits
z <- 0.5 + z + sqrt(.Machine$double.eps)
z <- trunc(z)
z <- z / 10^digits
z * x_sign
}

#' Format values as currencies
#'
#' With numeric values in a **gt** table, we can perform currency-based
Expand Down

0 comments on commit a0aa613

Please sign in to comment.