The function precision_prefix_ provides a suggested precision for use with SI prefix to use, given a value to format, and the difference between the values, step, that will be formatted. precision_prefix calculates the suggested SI Prefix given a numeric vector of values to format, x.

precision_prefix(x, prefix = NULL)

precision_prefix_(step, prefix)

Arguments

x
A numeric vector of values to be formatted.
prefix
The SI prefix to use. This is any valid argument for si_prefix. If NULL in precision_prefix, then the prefix is determined from the median value in x.
step
Numeric: Minimum absolute difference between values that will be formatted.

Source

The d3-format function https://github.com/d3/d3-format/blob/master/src/precisionPrefix.js.

Value

Named list of two elements:

precision
Integer vector: The suggested precision
si_prefix
An named integer vector of SI prefixes. The names are the SI prefix names, and the values are the exponents, as in SI_PREFIXES.

Details

The suggested precision, \(p\), when formatting a vector with an SI Prefix of \(10^k\) is, $$ p = \max \left( 0, k - \lfloor \log_{10}d \rfloor \right) ,$$ where \(d\) is the maximum absolute value between elements in the vector.

See also

precision_fixed for the suggested precision to use with formats that use a fixed number of digits after the decimal point, and precision_round for the suggested precision to use with formats that round to significant digits.

Examples

x <- c(1.1e6, 1.2e6, 1.3e6) p <- precision_prefix(x, 6L) p$precision
#> [1] 1
fmt(x, paste0(".", p$precision), si_prefix = 6L)
#> [1] "1.1M" "1.2M" "1.3M"