In R, add commas at right place in string of number

Viewed 22

We are looking to convert a long list of numbers into a currency format with dollar signs and commas. Convert c(10392, 3928403) into c('$10,392', '$3,928,403'). Adding the $ is easy but we are struggling to automate the adding of commas into the strings in the right locations (thousands separators). Is this possible in R?

We have:

> paste0('$', c(10392, 3928403))
[1] "$10392"   "$3928403"
1 Answers

A possible solution:

(Similar to @akrun's suggestions.)

scales::dollar(c(10392, 3928403))

#> [1] "$10,392"    "$3,928,403"
Related