1 /** 2 * 給數字左邊補0 3 */ 4 public static String completion0(int i){ 5 // 得到一個NumberFormat的實例 6 NumberFormat nf = NumberFormat.getInstance(); 7 // 設置是否使用分組 8 nf.setGroupingUsed(false); 9 // 設置最大整數位數 10 nf.setMaximumIntegerDigits(4); 11 // 設置最小整數位數 12 nf.setMinimumIntegerDigits(4); 13 // 輸出測試語句 14 return nf.format(i); 15 }
1 /** 2 * 數字轉字符串前面自動補0的實現 3 */ 4 public static String intToStr(int yourNumber) { 5 // 0 代表前面補充0 6 // 4 代表長度為4 7 // d 代表參數為正數型 8 String str = String.format("%04d", yourNumber); 9 return str; // 0001 10 }
1 /** 2 * 流水號加1后返回,流水號長度為4 3 */ 4 public static String haoAddOne_2(String liuShuiHao){ 5 Integer intHao = Integer.parseInt(liuShuiHao); 6 intHao++; 7 DecimalFormat df = new DecimalFormat("0000"); 8 return df.format(intHao); 9 }
By LiYing
