The cols_align_decimal() function

Let’s put together a two-column table to create a gt table. The first column char just contains letters whereas the second column, num, has a collection of numbers and NA values. We could format the numbers with fmt_number() and elect to drop the trailing zeros past the decimal mark with drop_trailing_zeros = TRUE. This can leave formatted numbers that are hard to scan through because the decimal mark isn’t fixed horizontally. We could remedy this and align the numbers by the decimal mark with cols_align_decimal().

dplyr::tibble(
  char = LETTERS[1:9],
  num = c(1.2, -33.52, 9023.2, -283.527, NA, 0.401, -123.1, NA, 41)
) |>
  gt() |>
  fmt_number(
    columns = num,
    decimals = 3,
    drop_trailing_zeros = TRUE
  ) |>
  cols_align_decimal()
char num
A     1.2  
B   −33.52 
C 9,023.2  
D  −283.527
E NA
F     0.401
G  −123.1  
H NA
I    41