Java Long Number Format formatLong(long value)

Here you can find the source of formatLong(long value)

Description

Formats a value using spaces to separate thousands as: ddd ddd ddd.

License

Open Source License

Parameter

Parameter Description
value the value to format

Return

the formatted value as a string

Declaration

public static String formatLong(long value) 

Method Source Code

//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&amp;nbsp;ddd&amp;nbsp;ddd</em> (with "&amp;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);
    }
}

Related

  1. format(Long value)
  2. format(long value)
  3. formatLong(long inLong, int inLen, boolean inComma, int inCommaPos)
  4. formatLong(Long number)
  5. formatLong(long val, int size)
  6. formatLong(long value)
  7. formatLong(long value, int length)
  8. formatLong(String value)
  9. formatLongAsDecimal(long l)