here's my approach from units to millions. digits parameter let me adjust the minimum number of significant values (integer + decimals). You could adjust decimal rounding inside first.
number <-function(number){
result <- if_else(
abs(number) < 1000000,
format(
number, digits = 3,
big.mark = ".",
decimal.mark = ","
),
paste0(
format(
number/1000000,
digits = 3,
drop0trailing = TRUE,
big.mark = ".",
decimal.mark = ","
),
"MM"
)
)
# result <- paste0("$", result)
return(result)
}