Skip to content

Instantly share code, notes, and snippets.

@matt-dray
Created May 28, 2024 19:54
Show Gist options
  • Save matt-dray/5a3e646a153d6ce94c3a860335a6300a to your computer and use it in GitHub Desktop.
Save matt-dray/5a3e646a153d6ce94c3a860335a6300a to your computer and use it in GitHub Desktop.
Quick function to split a number and convert it to an Excel-style numFmt
generate_num_format <- function(number = 8634567.890) {
num_val <- number %/% 1
num_dp <- number %% 1
num_split <- strsplit(as.character(number), "\\.")[[1]]
n_dp <- nchar(num_split[2])
num_commas <- scales::number(as.numeric(num_val), big.mark = ",")
fmt_out <- gsub("[[:digit:]]", "#", num_commas)
if (num_dp > 0) {
hash_dp <- paste0(rep("#", n_dp), collapse = "")
fmt_out <- paste0(fmt_out, ".", hash_dp)
}
fmt_out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment