The function precision_round_
takes the maximum absolute distance
between values, (step
), and the maximum absolute value, (value
),
of the values to be formatted, and returns the suggested precision for
formats that round to significant digits (e.g. "g"
).
precision_round
calculates this precision given a numeric vector of
values to format, x
.
precision_round(x) precision_round_(step, xmax)
The d3-format
function https://github.com/d3/d3-format/blob/master/src/precisionRound.js
An integer vector of suggest precisions. For precision_fixed
,
this is length one, for precision_fixed_
it is the same length
as step
.
The suggested precision, \(p\), for values in a vector, \(x\), is \( p = \max \left(0,\lfloor\log_{10}(|\max_{i} x|) \rfloor - \lfloor \log_{10} d \rfloor \right) \), where \(d\) the maximum absolute distance between values of \(x\).
For the exponential format, "e"
, substract use p - 1
for the
precision.
precision_fixed
for the suggested precision to
use with formats that use a fixed number of digits after the decimal point,
and precision_prefix
for a suggested SI prefix to use.
# For these, the step size is 0.01 and suggested precision is 3 x <- c(0.99, 1, 1.01) p <- precision_round(x) fmt(x, paste0(".", p, "r"))#> [1] "0.990" "1.00" "1.01"# For these, the step size is 0.1 and suggested precision is 2 x2 <- c(0.9, 1.0, 1.1) p2 <- precision_round(x2) fmt(x2, paste0(".", p2, "r"))#> [1] "0.90" "1.0" "1.1"#> [1] "9.90e-01" "1.00e+00" "1.01e+00"