按格式生成序號(hào),如0001,0002...9999
/***
? * 按格式生成序號(hào),如0001,0002...9999
? * @param idx 序號(hào)號(hào)碼
? * @param scale 位數(shù)
? * @return 按位數(shù)格式化的序號(hào),如0019
? */
?public static String buildSerial(int num,int scale){
??
??//格式化的位數(shù)小于等于0則拋出參數(shù)異常
??if(scale<=0)
???throw new IllegalArgumentException("scale:"+scale);
??
??//計(jì)算序列號(hào)的位數(shù)
??int count=0;
??int aIdx=num;
??while((aIdx=aIdx/10)>0)
???count++;
??count++;???
??
??//序列號(hào)的位數(shù)大于格式化位數(shù),或者序列號(hào)的值小于等于0時(shí),拋出參數(shù)異常
??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);
??//添加序列號(hào)值
??buf.append(num);
??return buf.toString();
?}
posted on 2006-07-17 00:53 hardson 閱讀(1159) 評(píng)論(0) 編輯 收藏 所屬分類: java基礎(chǔ)