int类型的数字  0,1,2,3
java里有直接将它转换成String 类型的: 000,001,002,003的函数和方法吗?请指教?就是不够位数用"0"补.

解决方案 »

  1.   

    public static String get(int value){
      if(Integer.toString(value).length==1)
        return "00"+Integer.toString(value);
      else if(Integer.toString(value).length==2)
        return "0"+Integer.toString(value);
      else
        return Integer.toString(value);
    }
      

  2.   

    int di = 1;  //这里假设值为1 也可以是从其他地方传过来的值
    String sr = ""+di;
    for(int i=sr.length;i<4;i++){  //只要没到4位就前加"0" 也可以换成任意N位
      sr = "0"+sr;

    System.out.println(sr);
      

  3.   

    /**
         * 以指定的字符左填充
         * 
         * @param s 需要填充的字串
         * @param len 填充后的长度
         * @param c 指定的填充字符
         * @return 左填充零后的字串
         */
        public static String padLeft(String s, int len, char c) {        if (s == null) {
                s = "";
            }
            s = s.trim();
            if (length(s) >= len) {
                return s;
            }        StringBuffer d = new StringBuffer(len);
            int fill = len - length(s);        while (fill-- > 0) {
                d.append(c);
            }        d.append(s);
            return d.toString();
        }
      

  4.   

    public static String change(int di){    //参数为要转换的数字
      String sr = ""+di;
      for(int i=sr.length;i<4;i++){         //只要没到4位就前加"0" 也可以换成任意N位
        sr = "0"+sr;
      }
      return sr;
    }
    调用这个方法就好了
      

  5.   

    public static String int2string(int i)
        {
            if(i < 10)
                return "00" + Integer.toString(i);
            else if(i < 100)
                return "0" + Integer.toString(i);
            else
                return Integer.toString(i);
        }
      

  6.   

    用DecimalFormat格式化就可以了。public class Test {
    public static void main(String[] args){
    java.text.DecimalFormat df=new java.text.DecimalFormat("000");
    for(int i=0;i<10;i++)
    System.out.println(df.format(i));
    }
    }
      

  7.   

    关于上面DecimalFormat详细的使用,lz可以参考sun的java API文档。
      

  8.   

    NumberFormat format = NumberFormat.getInstance();
    format.setMinimumIntegerDigits(3);
    public String format(int i)
    {
    return format.format(i);
    }
      

  9.   

    改进一下
    public static String change(int di){    //参数为要转换的数字
      String sr = ""+di;
      StringBuffer tempsr = new StringBuffer(sr);
      for(int i=tempsr.length();i<4;i++){   //只要没到4位就前加"0" 也可以换成任意N位
        tempsr.insert(0, "0");
      }
      return tempsr.toString();
    }
      

  10.   

    public class ChangeNumber {
      public static String change(int di,int bt){    
        String sr = ""+di;
        StringBuffer tempsr = new StringBuffer(sr);
        for(int i=tempsr.length();i<bt;i++){   //只要没到4位就前加"0" 也可以换成任意N位
          tempsr.insert(0, "0");
        }
        return tempsr.toString();
      }
      
      public static void main(String[] args) { 
         System.out.println(change(10,4));//第一个参数为要转换的数字 第二个参数为需要的位数
      }
    }
      

  11.   

    这个很简单,自已写一个小函数就是了
    还要找API不成?
      

  12.   

    to tricolors:
    难道你什么都自己写?幼稚!
      

  13.   

    上面CrazyGou(阿狗)(...) 给的也是一种不错的选择,呵呵
      

  14.   

    HOHO 楼上的大虾来了啊 我在好友里给你发信息了 看见了么
      

  15.   

    方法1,用String
    public String getStringInt(int n){
      String tmp = ""+n;
      while(tmp.length()<3){
        tmp = "0" + tmp;
      }
      return tmp;
    }
    方法2,用DecimalFormat
    public String getStringInt(int n){
      DecimalFormat df = new DecimalFormat("000");
      return df.format(n);
    }
      

  16.   

    可以试试apache的commons-lang包org.apache.commons.lang 
    Class StringUtils
    java.lang.Object
      |
      +--org.apache.commons.lang.StringUtilsIsEmpty/IsBlank - checks if a String contains text
    Trim/Strip - removes leading and trailing whitespace
    Equals - compares two strings null-safe
    IndexOf/LastIndexOf/Contains - null-safe index-of checks 
    IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut - index-of any of a set of Strings
    ContainsOnly/ContainsNone - does String contains only/none of these characters
    Substring/Left/Right/Mid - null-safe substring extractions
    SubstringBefore/SubstringAfter/SubstringBetween - substring extraction relative to other strings
    Split/Join - splits a String into an array of substrings and vice versa
    Remove/Delete - removes part of a String
    Replace/Overlay - Searches a String and replaces one String with another
    Chomp/Chop - removes the last part of a String
    LeftPad/RightPad/Center/Repeat - pads a String
    UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize - changes the case of a String
    CountMatches - counts the number of occurrences of one String in another
    IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable - checks the characters in a String
    DefaultString - protects against a null input String
    Reverse/ReverseDelimited - reverses a String
    Abbreviate - abbreviates a string using ellipsis
    Difference - compares two Strings and reports on their differences
    LevensteinDistance - the number of changes needed to change one String into another
      

  17.   

    int i = 1;
    NumberFormat df = NumberFormat.getInstance();
    df.setMinimumIntegerDigits(3);

    System.out.println(df.format(i));
      

  18.   

    imA(男的不会,会的不男) ( ) 信誉:3    Blog   加为好友  2007-4-24 15:37:36  得分:   
       
    用DecimalFormat格式化就可以了。public class Test {
    public static void main(String[] args){
    java.text.DecimalFormat df=new java.text.DecimalFormat("000");
    for(int i=0;i<10;i++)
    System.out.println(df.format(i));
    }
    }
    -------------------------------------
      
    厉害
      

  19.   

    收藏向CSDN的高手们致以节日问候!
      

  20.   

    public class TTest
    {    public static void main (String[] args)
        {
            System.out.println(addLeadingZero(7, 1));  //  7
            System.out.println(addLeadingZero(7, 3));  //  007
            System.out.println(addLeadingZero(17, 3)); //  017
        }
        
        private static String addLeadingZero (int $n, int $d)
        {
            String t = "";
            
            int i = (int)Math.log10($n) + 1;
            
            if ((i = ($d - i)) > 0) while (i-- > 0) t += '0';
            
            return t + $n;
        }
    }