Java Integer to String intToString(int value)

Here you can find the source of intToString(int value)

Description

int To String

License

Apache License

Declaration

static public String intToString(int value) 

Method Source Code

//package com.java2s;
/*/* w  w w  . j  a  v a  2s. co  m*/
 *  Copyright 2015 the original author or authors. 
 *  @https://github.com/scouter-project/scouter
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); 
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

public class Main {
    static public String intToString(int value) {
        StringBuilder buffer = new StringBuilder(20);
        String str = Integer.toString(value);

        int size = str.length();
        if (size <= 3)
            return str;

        int divide = size / 3;
        int remain = size % 3;
        buffer.append(str.substring(0, remain));

        int pos = 0;
        int index = 0;
        while (index < divide) {
            pos = remain + (3 * index);
            if (!(index == 0 && remain == 0))
                buffer.append(',');
            buffer.append(str.substring(pos, pos + 3));
            index++;
        }
        pos = remain + (3 * index);
        if (size > pos)
            buffer.append(',').append(str.substring(pos));

        return buffer.toString();
    }
}

Related

  1. intToString(int integer)
  2. intToString(int n)
  3. intToString(int theValue, int nDigits)
  4. intToString(int val, int width)
  5. IntToString(int value)
  6. intToString(int value, int nChars)
  7. intToString(int[] array)
  8. intToString(int[] values)
  9. intToString(String string)