Java Integer to String IntToString(int value)

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

Description

Int To String

License

Creative Commons License

Declaration

public static String IntToString(int value) 

Method Source Code

//package com.java2s;

public class Main {
    private static String Digits = "0123456789";

    public static String IntToString(int value) {
        if (value == Integer.MIN_VALUE) {
            return "-2147483648";
        }//from w  w w . ja v a 2  s.  co  m
        if (value == 0) {
            return "0";
        }
        boolean neg = value < 0;
        char[] chars = new char[24];
        int count = 0;
        if (neg) {
            chars[0] = '-';
            ++count;
            value = -value;
        }
        while (value != 0) {
            char digit = Digits.charAt((int) (value % 10));
            chars[count++] = digit;
            value /= 10;
        }
        if (neg) {
            ReverseChars(chars, 1, count - 1);
        } else {
            ReverseChars(chars, 0, count);
        }
        return new String(chars, 0, count);
    }

    private static void ReverseChars(char[] chars, int offset, int length) {
        int half = length >> 1;
        int right = offset + length - 1;
        for (int i = 0; i < half; i++, right--) {
            char value = chars[offset + i];
            chars[offset + i] = chars[right];
            chars[right] = value;
        }
    }
}

Related

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