在R中使用逗号分隔符和指定小数格式化数字 [英] Format number in R with both comma thousands separator and specified decimals

查看:600
本文介绍了在R中使用逗号分隔符和指定小数格式化数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用两个千位分隔符来格式化数字,并指定小数位数。例如,我使用 format per 为小数:

  FormatDecimal < -  function(x,k){
return(format(round(as.numeric(x),k),nsmall = k))
}
FormatDecimal(1000.64,1 )#1000.6

对于千位分隔符, formatC

  formatC(1000.64,big.mark =,)#1,001 






$ b pre $ p $ c> formatC(FormatDecimal(1000.64,1),big.mark =,)
#1000.6,因为不再是数字
formatC(round(as.numeric(1000.64),1),nsmall = 1,


$

如何获得 1,000.6



编辑:这个问题,询问关于格式化3.14为3,14(被标记为可能的dup) code>格式:格式:



格式(round(as.numeric(1000.64),1),nsmall = 1,big.mark =,)#1,000.6


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

这篇关于在R中使用逗号分隔符和指定小数格式化数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆