1.确定格式“00000000”,要求将数字按此格式进行左补齐
**例:此时输入的156 **
import org.apache.commons.lang.StringUtils;
import java.io.IOException;
public class Test{
public static void main(String[] args) throws IOException {
String num = "156";
String result = StringUtils.leftPad(num, 8, "0");
System.out.println(result);
}
}
结果为:"00000156"
2.将数字按照特定格式组合
例:现有随机8位以内数字, 目标 结果格式是"NUM00000000",那么该如何实现呢?
import java.io.IOException;
import java.text.DecimalFormat;
public class Test {
public static void main(String[] args) throws IOException {
DecimalFormat st = new DecimalFormat("NUM00000000");
String result = st.format(258);
System.out.println(result);
}
}
结果为 NUM00000258