Java Number Format format(Number number)

Here you can find the source of format(Number number)

Description

Returns a formatted string of the specified number.

License

Open Source License

Parameter

Parameter Description
number Number to convert to a string.

Return

A formatted string.

Declaration

public static String format(Number number) 

Method Source Code

//package com.java2s;
/* ******************************************************************************
 * Copyright (c) 2006-2013 XMind Ltd. and others.
 *
 * This file is a part of XMind 3. XMind releases 3 and
 * above are dual-licensed under the Eclipse Public License (EPL),
 * which is available at http://www.eclipse.org/legal/epl-v10.html
 * and the GNU Lesser General Public License (LGPL),
 * which is available at http://www.gnu.org/licenses/lgpl.html
 * See http://www.xmind.net/license.html for details.
 *
 * Contributors:// w w  w.ja va 2 s .c o m
 *     XMind Ltd. - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Returns a formatted string of the specified number. All trailing zeroes
     * or decimal points will be stripped.
     *
     * @param number
     *            Number to convert to a string.
     * @return A formatted string.
     */
    public static String format(Number number) {
        String formatted = Double.toString(number.doubleValue()).replaceAll("\\.0+$", "") //$NON-NLS-1$ //$NON-NLS-2$
                .replaceAll("(\\.[0-9]*[1-9])0+$", "$1"); //$NON-NLS-1$ //$NON-NLS-2$
        return formatted;
    }

    /**
     * Returns a formatted string of the specified object.
     *
     * @param number
     *            Object to convert to a string.
     * @return A formatted string.
     */
    public static String format(Object obj) {
        if (obj instanceof Number) {
            return format((Number) obj);
        } else {
            return obj.toString();
        }
    }
}

Related

  1. format(int decs, double number)
  2. format(int number, int length)
  3. format(String number)
  4. format(String number)
  5. formatAmountDigits(String numberString, int length)
  6. formatBinaryNumber(int number, int size)