Round a number and replace trailing zeros by spaces
Apparently, printf already has a feature for padding data to a specific length, see for example https://stackoverflow.com/q/34346389/1201032. So you can do:

f &lt;- function(x, n) {
  rounded_x &lt;- as.character(round(x, n))
  num_lead_chars &lt;- nchar(sub("[.].*", ".", rounded_x))
  sprintf("%-*s", n + num_lead_chars, rounded_x)
}


The function is vectorized for both inputs:

f(c(0.1, 0.1111999, 0.12, 0.1234), n = 4)
# [1] "0.1   " "0.1112" "0.12  " "0.1234"
f(0.1, n = 1:4)
# [1] "0.1"    "0.1 "   "0.1  "  "0.1   "