无论数字是多少,我都需要格式化与逗号符号左右固定宽度对齐的数字。例如,逗号左边是3位数,右边是2位数。因此12.3
格式为" 12.30"
。我想出的最佳解决方案是:
test <- c(10, 1, 0.1, 0.01, 0.001, 0)
format(round(c(100,test), 2),
justify = "right",
scientific = FALSE,
nsmall = 2)[-1]
有没有办法直接使用format
,printf
,sprintf
,...
答案 0 :(得分:1)
test <- c(10, 1, 0.1, 0.01, 0.001, 0)
formatC(test, width = 6, digits = 2, format = "f")
#[1] " 10.00" " 1.00" " 0.10" " 0.01" " 0.00" " 0.00"
显然,这是一种软宽度规格。如果左侧有三位以上的数字,则增加宽度以适应:
formatC(100000, width = 6, digits = 2, format = "f")
#[1] "100000.00"