number_format {scales}R Documentation

Number formatters

Description

A set of functions to format numeric 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.

Usage

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)
number(v, big.mark = ",")
number(v, accuracy = .001)
number(v, accuracy = .001, decimal.mark = ",")
number(v, accuracy = .5)

# number_format()
my_format <- number_format(big.mark = "'", decimal.mark = ",")
my_format(v)

# comma() and comma_format()
comma_format()(c(1, 1e3, 2000, 1e6))
comma_format(accuracy = .01)(c(1, 1e3, 2000, 1e6))
comma(c(1, 1e3, 2000, 1e6))

# percent() and percent_format()
percent_format()(runif(10))
percent(runif(10))

per_mille <- percent_format(
  scale = 1000,
  suffix = "\u2030",
  accuracy = .1
)
per_mille(.1234)

french_percent <- percent_format(
  decimal.mark = ",",
  suffix = " %"
)
french_percent(runif(10))

# 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)

# labels in hectares, raw data in square meters
ha <- unit_format(unit = "ha", scale = 1e-4)
km(runif(10) * 1e5)


[Package scales version 1.0.0 Index]