Wednesday, 16 December 2015

Java - Left padding a Number with Zeros

Left padding a Number with Zeros :
If  you have a number e.g num=1234 then you can directly use them in String.format() function, else if you have a String that contains only numeric value then convert them to integer and then use the String.format() function to do left padding.

Examples:
int yournumber = 67;
String.format("%05d", yournumber);
for zero-padding with length=5.

String mystring = "12";
String.format("%010d", Integer.parseInt(mystring));
for zero-padding with length=10.


No comments:

Post a Comment