Java Formatter Usage formatNumber(int number)

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

Description

This returns a formatted string that shows the number based on the locale turns 1000 into 1,000

License

Open Source License

Parameter

Parameter Description
number a parameter

Declaration

public static String formatNumber(int number) 

Method Source Code

//package com.java2s;
/*//  w  w w.  j  a  v a 2s .c  o  m
 * Copyright (c) Mirth Corporation. All rights reserved.
 * 
 * http://www.mirthcorp.com
 * 
 * The software in this package is published under the terms of the MPL license a copy of which has
 * been included with this distribution in the LICENSE.txt file.
 */

import java.util.Formatter;
import java.util.Locale;

public class Main {
    /**
     * This returns a formatted string that shows the number based on the locale
     * turns 1000 into 1,000
     * @param number
     * @return
     */
    public static String formatNumber(int number) {
        StringBuilder str = new StringBuilder();
        Formatter f = new Formatter(str, Locale.getDefault());
        f.format("%,d", number);
        return str.toString();
    }

    /**
     * This returns a formatted string that shows the number based on the locale
     * turns 1000 into 1,000
     * @param number
     * @return
     */
    public static String formatNumber(long number) {
        StringBuilder str = new StringBuilder();
        Formatter f = new Formatter(str, Locale.getDefault());
        f.format("%,d", number);
        return str.toString();
    }

    /**
     * Formats a number according to the locale
     * turns 1000.0 to 1,000
     * @param number
     * @return
     */
    public static String formatNumber(float number) {
        StringBuilder str = new StringBuilder();
        Formatter f = new Formatter(str, Locale.getDefault());
        f.format("%,.0f", number);
        return str.toString();
    }
}

Related

  1. format(String format, Object... args)
  2. format_hh_mm_ss_Optional(final long value)
  3. format_mm_ss(final long time)
  4. formatLong(long number, int length)
  5. formatMessageException(String format, Object... args)
  6. formatResponse(Formatter formatter, String response)
  7. formatTzname(final String format, final String letter)
  8. formatv(String format, Object[] args)
  9. formatZeroDecimals(Number x)