Skip to contents

Formats numbers for display in charts and tables. Supports abbreviation (e.g., thousands, millions), spelling out large numbers, adding percent or currency signs, and controlling decimal padding.

Usage

prettylabel(
  N,
  signif = 2,
  shorten = TRUE,
  spell = FALSE,
  padzero = FALSE,
  pct = FALSE,
  currency = NULL
)

Arguments

N

A number or numeric vector to be formatted.

signif

Number of significant digits to retain. Must be > 0. Default is 2.

shorten

Logical. If TRUE, abbreviates long numbers (e.g., 1,200 → "1.2K"). Currently not used directly but preserved for backward compatibility.

spell

Logical. If TRUE, spells out large values (e.g., "million" instead of "M").

padzero

Logical. If TRUE, pads decimals to reach the requested precision.

pct

Logical. If TRUE, appends a percent sign or " per cent" if spell = TRUE.

currency

Optional string (e.g., "$", "€") to prepend as a currency symbol.

Value

A character vector of formatted number labels.

Examples


prettylabel(123456)                      # "123K"
#> [1] "123K"

prettylabel(123456789, spell = TRUE)     # "123 million"
#> [1] "123 million"

prettylabel(0.756, pct = TRUE)           # "75.6%"
#> [1] "0.76%"

prettylabel(1200, currency = "$")        # "$1.2K"
#> [1] "$1.2K"

prettylabel(c(1e6, 2e9), signif = 3)     # "1.00M" "2.00B"
#> [1] "1M" "2B"

prettylabel(NA)                          # NA
#> [1] NA