Here you can find the source of formatNumber(int number)
Parameter | Description |
---|---|
number | a parameter |
public static String formatNumber(int number)
//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(); } }