目前的解决方案是
dp <- 2
sprintf(paste0("%.", dp, "f"), 0.123)
希望的解决方案没有 paste0() 并且类似于
sprintf("%.{%2$d}f", 0.123, 2L)
除了它的工作原理。
最佳答案
您可以使用 *
插入 dp
进入格式。
dp <- 2
sprintf("%.*f", dp, 0.123)
# [1] "0.12"
关于r - sprintf() : number of decimal places as an argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50232403/