用得着自己写吗?
好好看看java.text.DecimalFormat

解决方案 »

  1.   

    如果不能用java.text.DecimalFormat呢
      

  2.   

    为什么不能用java.text.DecimalFormat?如果非要自已写,看一下这个办法:public class Test
    {
        public static String format(int value)
        {
            StringBuffer buf = new StringBuffer();
            int count = 0;
            while(value > 10)
            {
                buf.append(value % 10);
                value /= 10;
                count++;
                if((count % 3) == 0)
                    buf.append(',');
            }
            buf.append(value);
            return buf.reverse().toString();
        }    public static void main(String[] args)
        {
            System.out.println(format(1));
            System.out.println(format(123));
            System.out.println(format(1234));
            System.out.println(format(123456));
            System.out.println(format(1234567));
            System.out.println(format(1234567890));
        }
    }
      

  3.   

    还有一个方法:public static String format(int value)
    {
        String strValue = String.valueOf(value);
        StringBuffer buf = new StringBuffer();
        int start = 0;
        int end = strValue.length() % 3;
        if(end == 0)
            end = 3;
        while(end < strValue.length())
        {
            buf.append(strValue.substring(start, end));
            buf.append(',');
            start = end;
            end += 3;           
        }
        buf.append(strValue.substring(start));
        return buf.toString();
    }
      

  4.   

    to:cbhyk() 
    你的函数当用1000测试时返回值是0100
      

  5.   

    while(value > 10)
    改成:
    while(value > 9)
    即可
      

  6.   

    或者
    while(value >= 10)
      

  7.   

    int 有正负之分,未考虑负数
      

  8.   

    import java.text.DecimalFormat;public class TestDecimalFormat{
    public static void main(String[] args){
    DecimalFormat df = new DecimalFormat();
    df.applyPattern("#,###,###");

    System.out.println(df.format(1234567890));
    System.out.println(df.format(-1234567890));
    }
    }
      

  9.   

    玩算法,参照javaclass Transform{
     final static char[] digits = {
    '0' , '1' , '2' , '3' , '4' , '5' ,
    '6' , '7' , '8' , '9'};
     final static char [] DigitTens = {
    '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
    '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
    '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
    '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
    '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
    '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
    '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
    '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
    '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
    '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
    } ; 
        final static char [] DigitOnes = { 
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    } ;

    public static String format(int i) {
    char separator = ',';
    char[] buf=new char[17];
            int q, r ,j=0;
            int charPos = 16;
            char sign = 0;        if (i < 0) { 
                sign = '-';
                i = -i;
            }        // Generate two digits per iteration
            while (i >= 65536) {
                q = i / 100;
            // really: r = i - (q * 100);
                r = i - ((q << 6) + (q << 5) + (q << 2));
                i = q;
                buf [--charPos] = DigitOnes[r];
                ++j;
                if(j == 3){
                 buf[--charPos] = separator;
                 j = 0;
                }
                buf [--charPos] = DigitTens[r];
                ++j;
                if(j == 3){
                 buf[--charPos] = separator;
                 j = 0;
                }
            }        // Fall thru to fast mode for smaller numbers
            // assert(i <= 65536, i);
            for (;;) { 
                q = (i * 52429) >>> (16+3);
                r = i - ((q << 3) + (q << 1));  // r = i-(q*10) ...            buf [--charPos] = digits [r];            i = q;
                if (i == 0) break;
                 ++j;
                if(j==3){
                 buf[--charPos] = separator;
                 j = 0;
                }   
             }
            if (sign != 0) 
                buf [--charPos] = sign;
          
            return new String(buf,charPos,17-charPos);
        }
    }
      

  10.   

    改一下int charPos = 17;