ProAnswers.org

left padding an int with zeros in java

How do you left pad an int with zeros in java when converting to a string?

I’m basically looking to pad out integers up to 9999 with the leading zeros.

E.g. 1 = “0001”

String.format("%05d", yournumber);

for zero-padding with length=5.



[http://download.oracle.com/javase/7/docs/api/java/util/Formatter.html](http://download.oracle.com/javase/7/docs/api/java/util/Formatter.html)