A set of functions to format numeric values:

  • number_format() and number() are generic formatters for numbers.

  • comma_format() and comma() format numbers with commas separating thousands.

  • percent_format() and percent() multiply values by one hundred and display percent sign.

  • unit_format() add units to the values.

All formatters allow you to re-scale (multiplicatively), to round to specified accuracy, to add custom suffix and prefix and to specify decimal.mark and big.mark.

number_format(accuracy = 1, scale = 1, prefix = "", suffix = "",
  big.mark = " ", decimal.mark = ".", trim = TRUE, ...)

number(x, accuracy = 1, scale = 1, prefix = "", suffix = "",
  big.mark = " ", decimal.mark = ".", trim = TRUE, ...)

comma_format(accuracy = 1, scale = 1, prefix = "", suffix = "",
  big.mark = ",", decimal.mark = ".", trim = TRUE, digits, ...)

comma(x, accuracy = 1, scale = 1, prefix = "", suffix = "",
  big.mark = ",", decimal.mark = ".", trim = TRUE, digits, ...)

percent_format(accuracy = NULL, scale = 100, prefix = "",
  suffix = "%", big.mark = " ", decimal.mark = ".", trim = TRUE,
  ...)

percent(x, accuracy = NULL, scale = 100, prefix = "",
  suffix = "%", big.mark = " ", decimal.mark = ".", trim = TRUE,
  ...)

unit_format(accuracy = 1, scale = 1, prefix = "", unit = "m",
  sep = " ", suffix = paste0(sep, unit), big.mark = " ",
  decimal.mark = ".", trim = TRUE, ...)

Arguments

accuracy

Number to round to, NULL for automatic guess.

scale

A scaling factor: x will be multiply by scale before formating (useful if the underlying data is on another scale, e.g. for computing percentages or thousands).

prefix, suffix

Symbols to display before and after value.

big.mark

Character used between every 3 digits to separate thousands.

decimal.mark

The character to be used to indicate the numeric decimal point.

trim

Logical, if FALSE, values are right-justified to a common width (see base::format()).

...

Other arguments passed on to base::format().

x

A numeric vector to format.

digits

Deprecated, use accuracy instead.

unit

The units to append.

sep

The separator between the number and the unit label.

Value

*_format() returns a function with single parameter x, a numeric vector, that returns a character vector.

Examples

# number() v <- c(12.3, 4, 12345.789, 0.0002) number(v)
#> [1] "12" "4" "12 346" "0"
number(v, big.mark = ",")
#> [1] "12" "4" "12,346" "0"
number(v, accuracy = .001)
#> [1] "12.300" "4.000" "12 345.789" "0.000"
number(v, accuracy = .001, decimal.mark = ",")
#> [1] "12,300" "4,000" "12 345,789" "0,000"
number(v, accuracy = .5)
#> [1] "12.5" "4.0" "12 346.0" "0.0"
# number_format() my_format <- number_format(big.mark = "'", decimal.mark = ",") my_format(v)
#> [1] "12" "4" "12'346" "0"
# comma() and comma_format() comma_format()(c(1, 1e3, 2000, 1e6))
#> [1] "1" "1,000" "2,000" "1,000,000"
comma_format(accuracy = .01)(c(1, 1e3, 2000, 1e6))
#> [1] "1.00" "1,000.00" "2,000.00" "1,000,000.00"
comma(c(1, 1e3, 2000, 1e6))
#> [1] "1" "1,000" "2,000" "1,000,000"
# percent() and percent_format() percent_format()(runif(10))
#> [1] "88.8%" "82.8%" "10.1%" "90.6%" "77.3%" "38.3%" "100.0%" "34.9%" #> [9] "94.7%" "21.6%"
percent(runif(10))
#> [1] "3.2%" "14.5%" "85.4%" "21.3%" "21.0%" "4.0%" "94.5%" "24.5%" "78.1%" #> [10] "28.8%"
per_mille <- percent_format( scale = 1000, suffix = "\u2030", accuracy = .1 ) per_mille(.1234)
#> [1] "123.4‰"
french_percent <- percent_format( decimal.mark = ",", suffix = " %" ) french_percent(runif(10))
#> [1] "87,5 %" "29,6 %" "98,4 %" "59,0 %" "75,9 %" "83,6 %" "76,3 %" "41,7 %" #> [9] "13,8 %" "8,1 %"
# unit_format() # labels in kilometer when the raw data are in meter km <- unit_format(unit = "km", scale = 1e-3, digits = 2) km(runif(10) * 1e3)
#> [1] "1 km" "1 km" "1 km" "0 km" "1 km" "1 km" "1 km" "1 km" "0 km" "1 km"
# labels in hectares, raw data in square meters ha <- unit_format(unit = "ha", scale = 1e-4) km(runif(10) * 1e5)
#> [1] "39 km" "5 km" "62 km" "60 km" "41 km" "86 km" "52 km" "98 km" "2 km" #> [10] "67 km"