The fmt_engineering() function

Let’s define a data frame that contains two columns of values (one small and one large). After creating a simple gt table from small_large_tbl we’ll call fmt_engineering() on both columns.

small_large_tbl <-
  dplyr::tibble(
    small = 10^(-12:-1),
    large = 10^(1:12)
  )

small_large_tbl |>
  gt() |>
  fmt_engineering()
small large
1.00 × 10−12 10.00
10.00 × 10−12 100.00
100.00 × 10−12 1.00 × 103
1.00 × 10−9 10.00 × 103
10.00 × 10−9 100.00 × 103
100.00 × 10−9 1.00 × 106
1.00 × 10−6 10.00 × 106
10.00 × 10−6 100.00 × 106
100.00 × 10−6 1.00 × 109
1.00 × 10−3 10.00 × 109
10.00 × 10−3 100.00 × 109
100.00 × 10−3 1.00 × 1012

Notice that within the form of m x 10^n, the n values move in steps of 3 (away from 0), and m values can have 1-3 digits before the decimal. Further to this, any values where n is 0 results in a display of only m (the first two values in the large column demonstrates this).

Engineering notation expresses values so that they are align to certain SI prefixes. Here is a table that compares select SI prefixes and their symbols to decimal and engineering-notation representations of the key numbers.

prefixes_tbl <-
  dplyr::tibble(
    name = c(
      "peta", "tera", "giga", "mega", "kilo",
      NA,
      "milli", "micro", "nano", "pico", "femto"
    ),
    symbol = c(
      "P", "T", "G", "M", "k",
      NA,
      "m", ":micro:", "n", "p", "f"
    ),
    decimal = c(10^(seq(15, -15, -3))),
    engineering = decimal
  )

prefixes_tbl |>
  gt() |>
  fmt_number(columns = decimal, n_sigfig = 1) |>
  fmt_engineering(columns = engineering) |>
  fmt_units(columns = symbol) |>
  sub_missing()
name symbol decimal engineering
peta P 1,000,000,000,000,000 1.00 × 1015
tera T 1,000,000,000,000 1.00 × 1012
giga G 1,000,000,000 1.00 × 109
mega M 1,000,000 1.00 × 106
kilo k 1,000 1.00 × 103
1 1.00
milli m 0.001 1.00 × 10−3
micro µ 0.000001 1.00 × 10−6
nano n 0.000000001 1.00 × 10−9
pico p 0.000000000001 1.00 × 10−12
femto f 0.000000000000001 1.00 × 10−15

The default method of styling the notation uses the ‘m x 10^n’ construction but this can be changed to a ‘mEn’ style via the exp_style argument. We can supply any single letter here and optionally affix a "1" to indicate there should not be any zero-padding of the n value. Two calls of fmt_engineering() are used here to show different options for styling in engineering notation.

small_large_tbl |>
  gt() |>
  fmt_engineering(
    columns = small,
    exp_style = "E"
  ) |>
  fmt_engineering(
    columns = large,
    exp_style = "e1",
    force_sign_n = TRUE
  )
small large
1.00E−12 10.00e+0
10.00E−12 100.00e+0
100.00E−12 1.00e+3
1.00E−09 10.00e+3
10.00E−09 100.00e+3
100.00E−09 1.00e+6
1.00E−06 10.00e+6
10.00E−06 100.00e+6
100.00E−06 1.00e+9
1.00E−03 10.00e+9
10.00E−03 100.00e+9
100.00E−03 1.00e+12