Current solution is
dp <- 2
sprintf(paste0("%.", dp, "f"), 0.123)
Hoped-for solution has no paste0() and is similar to
sprintf("%.{%2$d}f", 0.123, 2L)
Except that it works.
You can use a *
to insert dp
into the format.
dp <- 2
sprintf("%.*f", dp, 0.123)
# [1] "0.12"
Some other possibilities:
# option 1
prettyNum(0.123, digits = dp)
# option 2
formatC(0.123, digits = dp, format = 'f')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With