`
橡树心
  • 浏览: 46589 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

生成固定长度String型序列号,位数不足时补充0

 
阅读更多
public class IntString {   
    /**
	 * 自定义Int转String方法
	 * 
	 * @param num int型数值
	 * @param len 序列号长度
	 * @return String型固定长度序列号,位数不足时自动补0
	 */
	public static String toIntString(int num, int len) {
		String str = "";
		int s = len - sizeInt(num);
		for (int i = 0; i < s; i++) {
			str += "0";
		}
		return str + num;
	}
	/**
	 * 自定义查询Int型数据位数
	 * @param x Int型数据
	 * @return Int型数据位数
	 */
	public static int sizeInt(int x){
		final int[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,   
            99999999, 999999999, Integer.MAX_VALUE };    
        for (int i = 0;; i++)   
            if (x <= sizeTable[i])   
                return i + 1;    
	}   
    public static void main(String[] args) {   
        String s = toIntString(112,4);
		System.out.print(s);
    }   
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics