---
title: "生成固定长度String型序列号，位数不足时补充0"
author: "橡树心"
date: "2010-11-12T12:33:12Z"
tags: []
categories: ["编程语言"]
slug: "string-0"
draft: false
lastmod: "2010-11-12T12:33:12Z"
id: 809685
url: "https://oakfeeling.iteye.com/blog/809685"
---

```
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);
    }   
}
```