對於我的json編碼器,我想打印一組數字,其小數位數爲n
。兩種方法是:高性能數字格式
x <- c(1,2,pi)
n <- 2
format(x, digits = n, nsmall = n, trim = TRUE, drop0trailing = TRUE)
formatC(x, digits = n, format = "f", drop0trailing = TRUE)
然而drop0trailing
參數似乎引入大(〜10倍)業績倒退:
x <- rnorm(1e6)
system.time(format(x, digits = n, nsmall = n, trim = TRUE))
user system elapsed
0.584 0.000 0.584
system.time(format(x, digits = n, nsmall = n, trim = TRUE, drop0trailing = TRUE))
user system elapsed
5.763 0.040 5.799
有印數與n
小數是更快的另一種方式?
我可以阻止它形成跳躍到科幻符號? 'as.character(round(0.0001,4))' – Jeroen
@Jeroen查看我答案的更新。 –
@Jeroen我把'sprintf'和'sub'結合在了一起。查看更新。 –