Formatting numbers with leading zeroes using glue/str_glue

This seems like a pretty straightforward question, but my googling has so far failed me.

Is there a way, using glue/str_glue to format a number with leading zeroes, such that (for example), 1 is formatted as "01" or "001". In python, upon which this seems to be based, you can do "{var:02d}", but glue doesn't seem to like that. For example, this code:

num <- 1
numstr <- str_glue("{num:02d}")

Doesn't work. Nor do various permutations of that format string. In fact, placing a colon in the format string makes the formatter interpret it as a vector, so something like this:


results in:

I'm happy to just use sprintf and "%02d", but I'm already using glue for other stuff and it seems silly to mix them if I don't have to.


How about stringr::str_pad()?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.