Java Number Format formatNumber(int number)

Here you can find the source of formatNumber(int number)

Description

insert commas to the given number for the readability

License

Apache License

Parameter

Parameter Description
number a parameter

Declaration

public static String formatNumber(int number) 

Method Source Code

//package com.java2s;
/*--------------------------------------------------------------------------
 *  Copyright 2007 utgenome.org/*from   w ww.  j av a  2s.com*/
 *
 *  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 {
    /**
     * insert commas to the given number for the readability
     * 
     * @param number
     * @return
     */
    public static String formatNumber(int number) {
        StringBuilder s = new StringBuilder();
        String intValue = Integer.toString(number);

        final int len = intValue.length();
        for (int i = 0; i < len; ++i) {
            s.append(intValue.charAt(i));

            int digit = len - i - 1;
            if (digit != 0 && (digit % 3 == 0)) {
                s.append(',');
            }
        }
        return s.toString();
    }
}

Related

  1. formatNumber(double value)
  2. formatNumber(final long number)
  3. formatNumber(final String number)
  4. formatNumber(final StringBuilder buf, final long number)
  5. formatNumber(float num, int fragmentSize)
  6. formatNumber(int number, int zeros)
  7. formatNumber(int theNumber, int theNumberOfDigits)
  8. formatNumber(int value, int width, char c)
  9. formatNumber(Integer i, int bisectNumber)