Format number in R with both comma thousands separator and specified decimals

Max Ghenis

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).

Max Ghenis

format not formatC:

format(round(as.numeric(1000.64), 1), nsmall=1, big.mark=",") # 1,000.6

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

JavaScript regex to accept numbers with comma separator (thousands) and dot separator (decimals)

Is there any option to store a number as text with a specific format (comma as thousands separator and period as decimal separator) in Snowlake?

Format number with . as thousand separator and no decimals

R Plotly hooverinfo - adjust the number format to show thousands separator

python .format thousands separator in specified field width

Add a thousands separator with decimals

Format a decimal with comma as decimal separator and without thousands separator

Remove comma which is a thousands separator in R

Modify decimal point and thousands separator without changing the number of decimals

Is it possible to format number with thousands separator using fmt?

Format integer number with . as thousand separator and no decimals

How to format table1 values with comma as the thousands separator

NumberFormat with 0 decimals, but thousands separator " ' "

Oracle - Format number with fullstop for thousand and comma for decimals

Python regex for number with or without decimals using a dot or comma as separator?

Decimal dot and thousands separator used both in input type number

Add thousands separator in a number

Format number with thousands separator in Excel using Apache POI

How do I format axis number format to thousands with a comma in matplotlib?

r ggplot with zeroes and no comma as the big number separator

Add comma after specified length for number format

Format numbers with apostrophe as thousands separator

Regex to separate thousands with comma and keep two decimals

R Custom Number Format: Thousands, Millions, Billions

Number formatting, remove thousands separator

Adding thousands separator "˙" to a float number

How to format a calculated number result with thousand comma separator

Adjust thousands separator to comma with Plotly library in Python

How to format dygraphs labels in R - comma separate thousands place?