JAVA组合字符串,长度不足用0补齐

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Test {
public static void main(String[] args) {
Test test = new Test();
int num = 16;
System.out.println("before : " + num);
System.out.println("after : " + test.lpad(10, num));
}

/**
* 补齐不足长度
*
* @param length 长度
* @param number 数字
* @return
*/
private String lpad(int length, int number) {
String f = "%0" + length + "d";
return String.format(f, number);
}
}

结果:

1
2
before : 16
after : 0000000016