Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format numbers to same number of digits, 0-padded?

Tags:

java

format

I have several strings of different length

"2323"
"245"
"353352"

I need to convert them to string of the same size such as:

"0002323"
"0000245"
"0353352"

How can I automatically add the correct number of 0s in front of each string ?

thanks

like image 520
aneuryzm Avatar asked Mar 23 '11 09:03

aneuryzm


1 Answers

Use String.format:

Integer i = Integer.valueOf(src);
String format = "%1$07d";
String result = String.format(format, i);
like image 112
Vladimir Ivanov Avatar answered Nov 11 '22 00:11

Vladimir Ivanov