Java Number Format Pattern getNumberFormat(String format, Locale locale)

Here you can find the source of getNumberFormat(String format, Locale locale)

Description

Returns a NumberFormat instance for the specified format and Locale .

License

LGPL

Parameter

Parameter Description
format the custom or standard formatting pattern to be used
locale the Locale to be used

Return

an instance of

Declaration

public static NumberFormat getNumberFormat(String format, Locale locale) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.text.*;
import java.util.*;

public class Main {
    private static final int STYLE_NUMBER = 0;
    private static final int STYLE_CURRENCY = 1;
    private static final int STYLE_PERCENT = 2;
    private static final int STYLE_INTEGER = 4;

    /**/*w w w. j  a va2 s. c o m*/
     * Returns a {@link NumberFormat} instance for the specified
     * format and {@link Locale}.  If the format specified is a standard
     * style pattern, then a number instance
     * will be returned with the number style set to the
     * specified style.  If it is a custom format, then a customized
     * {@link NumberFormat} will be returned.
     *
     * @param format the custom or standard formatting pattern to be used
     * @param locale the {@link Locale} to be used
     * @return an instance of {@link NumberFormat}
     * @see NumberFormat
     */
    public static NumberFormat getNumberFormat(String format, Locale locale) {
        if (format == null || locale == null) {
            return null;
        }

        NumberFormat nf = null;
        int style = getNumberStyleAsInt(format);
        if (style < 0) {
            // we have a custom format
            nf = new DecimalFormat(format, new DecimalFormatSymbols(locale));
        } else {
            // we have a standard format
            nf = getNumberFormat(style, locale);
        }
        return nf;
    }

    /**
     * Returns a {@link NumberFormat} instance for the specified
     * number style and {@link Locale}.
     *
     * @param numberStyle the number style (number will be ignored if this is
     *                    less than zero or the number style is not recognized)
     * @param locale      the {@link Locale} to be used
     * @return an instance of {@link NumberFormat} or <code>null</code>
     *         if an instance cannot be constructed with the given
     *         parameters
     */
    public static NumberFormat getNumberFormat(int numberStyle, Locale locale) {
        try {
            NumberFormat nf;
            switch (numberStyle) {
            case STYLE_NUMBER:
                nf = NumberFormat.getNumberInstance(locale);
                break;
            case STYLE_CURRENCY:
                nf = NumberFormat.getCurrencyInstance(locale);
                break;
            case STYLE_PERCENT:
                nf = NumberFormat.getPercentInstance(locale);
                break;
            case STYLE_INTEGER:
                nf = NumberFormat.getIntegerInstance(locale);
                break;
            default:
                // invalid style was specified, return null
                nf = null;
            }
            return nf;
        } catch (Exception suppressed) {
            // let it go...
            return null;
        }
    }

    /**
     * Checks a string to see if it matches one of the standard
     * NumberFormat style patterns:
     * number, currency, percent, integer, or default.
     * if it does it will return the integer constant for that pattern.
     * if not, it will return -1.
     *
     * @param style the string to be checked
     * @return the int identifying the style pattern
     * @see NumberFormat
     */
    public static int getNumberStyleAsInt(String style) {
        // avoid needlessly running through all the string comparisons
        if (style == null || style.length() < 6 || style.length() > 8) {
            return -1;
        }
        if (style.equalsIgnoreCase("default")) {
            //NOTE: java.text.NumberFormat returns "number" instances
            //      as the default (at least in Java 1.3 and 1.4).
            return STYLE_NUMBER;
        }
        if (style.equalsIgnoreCase("number")) {
            return STYLE_NUMBER;
        }
        if (style.equalsIgnoreCase("currency")) {
            return STYLE_CURRENCY;
        }
        if (style.equalsIgnoreCase("percent")) {
            return STYLE_PERCENT;
        }
        if (style.equalsIgnoreCase("integer")) {
            return STYLE_INTEGER;
        }
        // ok, it's not any of the standard patterns
        return -1;
    }
}

Related

  1. getNumberFormat()
  2. getNumberFormat(BigDecimal num, final int digits)
  3. getNumberFormat(double d)
  4. getNumberFormat(final String pattern)
  5. getNumberFormat(int maximumFractionDigits)
  6. getNumberFormat(String localeStr)
  7. getNumberFormatFromCache(Locale locale, Currency currency)
  8. getNumberFormatInstanceFor2DecimalDigits()
  9. getNumberFormatter()