I'd like to format numbers with both thousands separator and specifying the number of decimals. I know how to do these separately, but not together.
For example, I use format per this for the decimals:
FormatDecimal <- function(x, k) { return(format(round(as.numeric(x), k), nsmall=k)) } FormatDecimal(1000.64, 1) # 1000.6 And for thousands separator, formatC:
formatC(1000.64, big.mark=",") # 1,001 These don't play nicely together though:
formatC(FormatDecimal(1000.64, 1), big.mark=",") # 1000.6, since no longer numeric formatC(round(as.numeric(1000.64), 1), nsmall=1, big.mark=",") # Error: unused argument (nsmall=1) How can I get 1,000.6?
Edit: This differs from this question which asks about formatting 3.14 as 3,14 (was flagged as possible dup).
format not formatC:
format(round(as.numeric(1000.64), 1), nsmall=1, big.mark=",") # 1,000.6
formatC(1000.64, format="f", big.mark=",", digits=1) (sorry if i'm missing something.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With