Java Number Format formatNumberWithKSeparators(int value)

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

Description

Formats the number in value with thousand separator "," for display.

License

LGPL

Parameter

Parameter Description
value a parameter

Declaration

public static String formatNumberWithKSeparators(int value) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    /**//from  w w w .jav a2  s  . c om
     * Formats the number in value with thousand separator "," for display.
     * @param value
     * @return
     */
    public static String formatNumberWithKSeparators(int value) {
        StringBuilder sb = new StringBuilder(16);

        String number = String.valueOf(value);
        int len = number.length();
        int end = len % 3, i = 0;

        sb.append(number.substring(0, end));

        for (i = end; i <= (len - 3); i += 3) {
            if (i > 0) {
                sb.append(",");
            }

            sb.append(number.substring(i, i + 3));
        }

        return sb.toString();
    }
}

Related

  1. formatNumber(String value)
  2. formatNumberFloorWithPostfix(int value)
  3. formatNumberImpl(long i, int digits, String fill)
  4. formatNumbers(final int... numbers)
  5. formatNumberWithExtension(int num)
  6. formatNumberWithSameWidth(int d)
  7. formatOnlyLettersNumbersAndSpaces(String keyword)
  8. formatPaddedNumber(long number, int numericPadding)
  9. formatPartialName(String name, int numberOfCharacters)