This returns a formatted string that shows the number based on the locale turns 1000 into 1,000 - Java java.lang

Java examples for java.lang:double Format

Description

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

Demo Code

/*/*ww  w.  j  av a 2  s.com*/
 * 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.
 */
//package com.java2s;
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 Tutorials