Java Locale Format formatNumber(Object value, String numberFormat)

Here you can find the source of formatNumber(Object value, String numberFormat)

Description

Format a number using a valid Java format mask and the default Locale

License

LGPL

Parameter

Parameter Description
value Double, Integer or another numeric value
numberFormat Java numeric format mask like #,##0.00

Exception

Parameter Description
Throwable an exception

Return

String representing a formatted number acording to the numberFormat

Declaration

public static String formatNumber(Object value, String numberFormat) throws Throwable 

Method Source Code

//package com.java2s;
/**/*from   w ww .  ja  va 2 s.  c o  m*/
 * Core-level framework class: String and Date basic utility methods.
 * <br><br>
 * Encapsulates utility methods for everyday programming tasks
 * with Strings, Dates and other common stuff.
 * <br>
 * Creation date: 18/09/2003<br>
 * Last Update: 18/09/2003<br>
 * (c) 2003 Martin Cordova<br>
 * This code is released under the LGPL license<br>
 * @author Martin Cordova (some code written by Carlos Pineda)
 */

import java.text.DecimalFormat;
import java.text.NumberFormat;

import java.util.Locale;
import java.util.Map;

public class Main {
    /**
     * Format a number using a valid Java format mask and the default Locale
     * @param value Double, Integer or another numeric value
     * @param numberFormat Java numeric format mask like #,##0.00
     * @return String representing a formatted number acording to the numberFormat
     * @throws Throwable
     */
    public static String formatNumber(Object value, String numberFormat) throws Throwable {
        DecimalFormat fmt = (DecimalFormat) NumberFormat.getInstance();
        fmt.applyPattern(numberFormat);
        return fmt.format(value);
    }

    /**
     * Format a number using a valid Java format mask and a custom Locale
     * @param value Double, Integer or another numeric value
     * @param numberFormat Java numeric format mask like #,##0.00
     * @param loc Custom Locale to use when formatting the number
     * @return String representing a formatted number acording to the numberFormat
     * @throws Throwable
     */
    public static String formatNumber(Object value, String numberFormat, Locale loc) throws Throwable {
        DecimalFormat fmt = (DecimalFormat) NumberFormat.getInstance(loc);
        fmt.applyPattern(numberFormat);
        return fmt.format(value);
    }

    public static String format(String pattern, Map<String, Object> arguments) {
        String formatedStr = pattern;
        for (String key : arguments.keySet()) {
            formatedStr = formatedStr.replaceAll("\\{:" + key + "\\}", arguments.get(key).toString());
        }
        return formatedStr;
    }
}

Related

  1. formatNumber(Number number)
  2. formatNumber(Number value, int minDecimals, int maxDecimals, boolean grouping, boolean dotDecimalSymbol)
  3. formatNumber(Number value, String format)
  4. formatNumber(Object number, String pattern, Locale locale)
  5. formatNumber(Object value, Locale locale)
  6. formatNumberSameWidth(final double v)
  7. formatNumberUk(double inNumber, int inDecimalPlaces)
  8. formatNumberWithThousandSeparator(long number)
  9. formatPCT(Object num)