Web Programming

Only members of this group can post notes and use other group resources. Join now to get access to all group resources!
Log in or register to join this group

Java/JSP Left padding integers with zeros in Java  Viewed 23714 time(s), 3 post(s)., 12/31/2013 5:21:03 AM - by Mike

Mike
Mike 12/31/2013 5:21:03 AM

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

for example: 1 = "0001"

This content has not been rated yet. 
51 Reputation 3 Total posts
1
Trika
Trika 1/1/2014 7:50:20 PM

You may use String.format

for example
String.format("%04", intNum);

This content has not been rated yet. 
53 Reputation 5 Total posts
2
Trika
Trika 1/1/2014 7:51:20 PM

Also, you may try DecimalFormat

This content has not been rated yet. 
53 Reputation 5 Total posts
3