Here you can find the source of formatNumber(final long number)
Parameter | Description |
---|---|
number | The number to format |
public static String formatNumber(final long number)
//package com.java2s; /*********************************************************************************** * /*from w w w.j a va2 s . c o m*/ * Copyright (c) 2014 Kamil Baczkowicz * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * * Kamil Baczkowicz - initial API and implementation and/or initial documentation * */ public class Main { /** * Formats the given number into one where thousands are separated by a space. * * @param number The number to format * @return Formatted string (e.g. "1 315 124" for 1315124) */ public static String formatNumber(final long number) { long divided = number; final StringBuffer sb = new StringBuffer(); while (divided > 1000) { long rest = divided % 1000; sb.insert(0, " " + String.format("%03d", rest)); divided = divided / 1000; } long rest = divided % 1000; sb.insert(0, rest); return sb.toString(); } }