Here you can find the source of formatLong(long value)
Parameter | Description |
---|---|
value | the value to format |
public static String formatLong(long value)
//package com.java2s; public class Main { /**/*ww w . ja v a2s . c om*/ * Formats a value using spaces to separate thousands as: * <em>ddd ddd ddd</em>. * * @param value the value to format * @return the formatted value as a string */ public static String formatLong(long value) { boolean isNegative = value < 0; if (isNegative) { value = -value; } // -9 223 372 036 854 775 808 char[] buffer = new char[1 + 19 + 6]; int index = buffer.length - 1; if (value == 0) { buffer[index--] = '0'; } else { for (int count = 0; value > 0 && index >= 0; count++) { if (((count % 3) == 0) && count > 0 && index > 0) { buffer[index--] = ' '; } buffer[index--] = (char) ('0' + (value % 10)); value /= 10; } } if (isNegative && index >= 0) { buffer[index--] = '-'; } return new String(buffer, index + 1, buffer.length - index - 1); } /** * Formats a value using the specified separator to separating thousands as: * <em>ddd&nbsp;ddd&nbsp;ddd</em> (with "&nbsp;" as separator). * * @param value the value to format * @param separator the separator to use * @return the formatted value as a string */ public static String formatLong(long value, String separator) { boolean isNegative = value < 0; if (isNegative) { value = -value; } // -9 223 372 036 854 775 808 int sepLen = separator.length(); int maxLen = 1 + 19 + 6 * sepLen; char[] buffer = new char[maxLen]; int index = maxLen - 1; if (value == 0) { buffer[index--] = '0'; } else { for (int count = 0; value > 0 && index >= 0; count++) { if (((count % 3) == 0) && count > 0 && index > sepLen) { index -= sepLen; separator.getChars(0, sepLen, buffer, index + 1); } buffer[index--] = (char) ('0' + (value % 10)); value /= 10; } } if (isNegative && index >= 0) { buffer[index--] = '-'; } return new String(buffer, index + 1, maxLen - index - 1); } }