R/number-format-functions.R

Defines functions fmt_pretty fmt_percent

Documented in fmt_percent fmt_pretty

#' Description for `fmt_` functions
#'
#' @description
#' `fmt_` functions are designed to work with [number_format()].
#'
#' @return An object you can pass into [number_format()].
#' @name fmt-numbers
#' @keywords internal
NULL

#' Format numbers as percent
#'
#'
#' @param digits How many digits to print.
#' @param format,... Passed into [formatC()].
#'
#' @inherit fmt-numbers description return
#'
#' @export
#'
#' @family format functions
#'
#' @examples
#'
#' jams$Sugar <- c(
#'   "Sugar content",
#'   0.4, 0.35, 0.45
#' )
#' set_number_format(
#'   jams, -1, "Sugar",
#'   fmt_percent(1)
#' )
#'
fmt_percent <- function(digits = 1, format = "f", ...) {
  list(function(x) {
    paste0(formatC(round(x * 100, digits), format = format, ..., digits = digits), "%")
  })
}


#' Use `prettyNum()` to format numbers
#'
#' @param big.mark,scientific,... Passed to [prettyNum()].
#'
#' @inherit fmt-numbers return
#'
#' @export
#'
#' @family format functions
#'
#' @examples
#'
#' jams$Sales <- c(
#'   "Sales", 35000,
#'   55500, 20000
#' )
#' set_number_format(
#'   jams, -1, "Sales",
#'   fmt_pretty()
#' )
#'
fmt_pretty <- function(big.mark = ",", ..., scientific = FALSE) {
  list(function(x) {
    prettyNum(x, big.mark = big.mark, scientific = scientific, ...)
  })
}

Try the huxtable package in your browser

Any scripts or data that you put into this service are public.

huxtable documentation built on Nov. 7, 2025, 9:07 a.m.