public static void main(String[] args) { NumberFormat formatter1 = NumberFormat.getNumberInstance(); //5位数,前面补0 formatter1.setMinimumIntegerDigits(5); formatter1.setGroupingUsed(false); String format = formatter1.format(56); System.out.println(format); //00056 }
方法2:
public static String getNewFileName(String oldName, int len) { StringBuffer buffer = new StringBuffer(oldName); while (buffer.length() < len) { buffer.insert(0, 0); // 补0 } return buffer.toString(); } public static void main(String[] args) { System.out.println(getNewFileName("56", 6)); }