/***
? * 按格式生成序號,如0001,0002...9999
? * @param idx 序號號碼
? * @param scale 位數
? * @return 按位數格式化的序號,如0019
? */
?public static String buildSerial(int num,int scale){
??
??//格式化的位數小于等于0則拋出參數異常
??if(scale<=0)
???throw new IllegalArgumentException("scale:"+scale);
??
??//計算序列號的位數
??int count=0;
??int aIdx=num;
??while((aIdx=aIdx/10)>0)
???count++;
??count++;???
??
??//序列號的位數大于格式化位數,或者序列號的值小于等于0時,拋出參數異常
??if(count>scale || num<=0)
???throw new IllegalArgumentException("idx:"+num);
?
??//在序列前空出的位上添加0
??StringBuffer buf=new StringBuffer(scale);?????
??for(int i=scale-count;--i>=0;)
???buf.append(0);
??//添加序列號值
??buf.append(num);
??return buf.toString();
?}