Thanks Denver, got your point. I'm not familiar with the sprintf function.
/gustav
I've never seen anything else than unsigned digitals applied leading zeroes and the question - with 5 as the example - didn't indicate a uncommon situation.
The example was sprintf with a %04d format.
If you want to emulate it exactly, across the domain of real numbers or strings,
then Timur's code won't do it.
But whether it works depends entirely on the domain. The domain cannot be inferred from a single element.
Bugs rarely show up in the common situations.
That's why I pointed out that it was not universally able to emulate %04d.
I left it up to you to consider your domain.
Hey, two digit years worked for all the common situations.
How bullet-proof does your code have to be?
I seen lots of values show up that the original programmers never expected.
Small negative numbers aren't exactly uncommon.
