Java Code Examples for java.text.DecimalFormat#applyLocalizedPattern()

The following examples show how to use java.text.DecimalFormat#applyLocalizedPattern() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BasisLibrary.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static String formatNumber(double number, String pattern,
                                  DecimalFormat formatter) {
    // bugzilla fix 12813
    if (formatter == null) {
        formatter = defaultFormatter;
    }
    try {
        StringBuffer result = threadLocalStringBuffer.get();
    result.setLength(0);
        if (pattern != defaultPattern) {
            formatter.applyLocalizedPattern(pattern);
        }
    formatter.format(number, result, _fieldPosition);
        return result.toString();
    }
    catch (IllegalArgumentException e) {
        runTimeError(FORMAT_NUMBER_ERR, Double.toString(number), pattern);
        return(EMPTYSTRING);
    }
}
 
Example 2
Source File: SakaiNumberFormatProvider.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Returns a new NumberFormat instance for the specified key and locale.
 *
 * @param key the property key.
 * @param locale the desired locale.
 * @return a number formatter.
 * @throws IllegalArgumentException if <code>locale</code> isn't available.
 * @throws NullPointerException if <code>locale</code> is <code>null</code>.
 */
protected NumberFormat getInstance(final String key, final Locale locale) throws IllegalArgumentException,
		NullPointerException {
	if (locale == null) {
		throw new NullPointerException("locale:null");
	} else if (!SakaiLocaleServiceProviderUtil.isAvailableLocale(locale)) {
		throw new IllegalArgumentException("locale:" + locale.toString());
	}

	DecimalFormat format = new DecimalFormat();
	format.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(locale));

	String pattern = SakaiLocaleServiceProviderUtil.getString(key, locale);
	format.applyLocalizedPattern(pattern);

	return format;
}
 
Example 3
Source File: StringLocaleConverter.java    From commons-beanutils with Apache License 2.0 6 votes vote down vote up
/**
 * Make an instance of DecimalFormat.
 *
 * @param locale The locale
 * @param pattern The pattern is used for the conversion
 * @return The format for the locale and pattern
 *
 * @throws ConversionException if conversion cannot be performed
 *  successfully
 * @throws IllegalArgumentException if an error occurs parsing a String to a Number
 */
private DecimalFormat getDecimalFormat(final Locale locale, final String pattern) {

    final DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getInstance(locale);

    // if some constructors default pattern to null, it makes only sense to handle null pattern gracefully
    if (pattern != null) {
        if (locPattern) {
            numberFormat.applyLocalizedPattern(pattern);
        } else {
            numberFormat.applyPattern(pattern);
        }
    } else {
        log.debug("No pattern provided, using default.");
    }

    return numberFormat;
}
 
Example 4
Source File: SakaiNumberFormatProvider.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Returns a new NumberFormat instance for the specified key and locale.
 *
 * @param key the property key.
 * @param locale the desired locale.
 * @return a number formatter.
 * @throws IllegalArgumentException if <code>locale</code> isn't available.
 * @throws NullPointerException if <code>locale</code> is <code>null</code>.
 */
protected NumberFormat getInstance(final String key, final Locale locale) throws IllegalArgumentException,
		NullPointerException {
	if (locale == null) {
		throw new NullPointerException("locale:null");
	} else if (!SakaiLocaleServiceProviderUtil.isAvailableLocale(locale)) {
		throw new IllegalArgumentException("locale:" + locale.toString());
	}

	DecimalFormat format = new DecimalFormat();
	format.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(locale));

	String pattern = SakaiLocaleServiceProviderUtil.getString(key, locale);
	format.applyLocalizedPattern(pattern);

	return format;
}
 
Example 5
Source File: BasisLibrary.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public static String formatNumber(double number, String pattern,
                                  DecimalFormat formatter) {
    // bugzilla fix 12813
    if (formatter == null) {
        formatter = defaultFormatter;
    }
    try {
        StringBuffer result = threadLocalStringBuffer.get();
    result.setLength(0);
        if (pattern != defaultPattern) {
            formatter.applyLocalizedPattern(pattern);
        }
    formatter.format(number, result, _fieldPosition);
        return result.toString();
    }
    catch (IllegalArgumentException e) {
        runTimeError(FORMAT_NUMBER_ERR, Double.toString(number), pattern);
        return(EMPTYSTRING);
    }
}
 
Example 6
Source File: BasisLibrary.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public static String formatNumber(double number, String pattern,
                                  DecimalFormat formatter) {
    // bugzilla fix 12813
    if (formatter == null) {
        formatter = defaultFormatter;
    }
    try {
        StringBuffer result = threadLocalStringBuffer.get();
    result.setLength(0);
        if (pattern != defaultPattern) {
            formatter.applyLocalizedPattern(pattern);
        }
    formatter.format(number, result, _fieldPosition);
        return result.toString();
    }
    catch (IllegalArgumentException e) {
        runTimeError(FORMAT_NUMBER_ERR, Double.toString(number), pattern);
        return(EMPTYSTRING);
    }
}
 
Example 7
Source File: BasisLibrary.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static String formatNumber(double number, String pattern,
                                  DecimalFormat formatter) {
    // bugzilla fix 12813
    if (formatter == null) {
        formatter = defaultFormatter;
    }
    try {
        StringBuffer result = threadLocalStringBuffer.get();
    result.setLength(0);
        if (pattern != defaultPattern) {
            formatter.applyLocalizedPattern(pattern);
        }
    formatter.format(number, result, _fieldPosition);
        return result.toString();
    }
    catch (IllegalArgumentException e) {
        runTimeError(FORMAT_NUMBER_ERR, Double.toString(number), pattern);
        return(EMPTYSTRING);
    }
}
 
Example 8
Source File: BasisLibrary.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static String formatNumber(double number, String pattern,
                                  DecimalFormat formatter) {
    // bugzilla fix 12813
    if (formatter == null) {
        formatter = defaultFormatter;
    }
    try {
        StringBuffer result = threadLocalStringBuffer.get();
    result.setLength(0);
        if (pattern != defaultPattern) {
            formatter.applyLocalizedPattern(pattern);
        }
    formatter.format(number, result, _fieldPosition);
        return result.toString();
    }
    catch (IllegalArgumentException e) {
        runTimeError(FORMAT_NUMBER_ERR, Double.toString(number), pattern);
        return(EMPTYSTRING);
    }
}
 
Example 9
Source File: BasisLibrary.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
public static String formatNumber(double number, String pattern,
                                  DecimalFormat formatter) {
    // bugzilla fix 12813
    if (formatter == null) {
        formatter = defaultFormatter;
    }
    try {
        StringBuffer result = threadLocalStringBuffer.get();
    result.setLength(0);
        if (pattern != defaultPattern) {
            formatter.applyLocalizedPattern(pattern);
        }
    formatter.format(number, result, _fieldPosition);
        return result.toString();
    }
    catch (IllegalArgumentException e) {
        runTimeError(FORMAT_NUMBER_ERR, Double.toString(number), pattern);
        return(EMPTYSTRING);
    }
}
 
Example 10
Source File: BasisLibrary.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static String formatNumber(double number, String pattern,
                                  DecimalFormat formatter) {
    // bugzilla fix 12813
    if (formatter == null) {
        formatter = defaultFormatter;
    }
    try {
        StringBuffer result = threadLocalStringBuffer.get();
    result.setLength(0);
        if (pattern != defaultPattern) {
            formatter.applyLocalizedPattern(pattern);
        }
    formatter.format(number, result, _fieldPosition);
        return result.toString();
    }
    catch (IllegalArgumentException e) {
        runTimeError(FORMAT_NUMBER_ERR, Double.toString(number), pattern);
        return(EMPTYSTRING);
    }
}
 
Example 11
Source File: BasisLibrary.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static String formatNumber(double number, String pattern,
                                  DecimalFormat formatter) {
    // bugzilla fix 12813
    if (formatter == null) {
        formatter = defaultFormatter;
    }
    try {
        StringBuffer result = threadLocalStringBuffer.get();
    result.setLength(0);
        if (pattern != defaultPattern) {
            formatter.applyLocalizedPattern(pattern);
        }
    formatter.format(number, result, _fieldPosition);
        return result.toString();
    }
    catch (IllegalArgumentException e) {
        runTimeError(FORMAT_NUMBER_ERR, Double.toString(number), pattern);
        return(EMPTYSTRING);
    }
}
 
Example 12
Source File: BasisLibrary.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
public static String formatNumber(double number, String pattern,
                                  DecimalFormat formatter) {
    // bugzilla fix 12813
    if (formatter == null) {
        formatter = defaultFormatter;
    }
    try {
        StringBuffer result = threadLocalStringBuffer.get();
    result.setLength(0);
        if (pattern != defaultPattern) {
            formatter.applyLocalizedPattern(pattern);
        }
    formatter.format(number, result, _fieldPosition);
        return result.toString();
    }
    catch (IllegalArgumentException e) {
        runTimeError(FORMAT_NUMBER_ERR, Double.toString(number), pattern);
        return(EMPTYSTRING);
    }
}
 
Example 13
Source File: BasisLibrary.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static String formatNumber(double number, String pattern,
                                  DecimalFormat formatter) {
    // bugzilla fix 12813
    if (formatter == null) {
        formatter = defaultFormatter;
    }
    try {
        StringBuffer result = threadLocalStringBuffer.get();
    result.setLength(0);
        if (pattern != defaultPattern) {
            formatter.applyLocalizedPattern(pattern);
        }
    formatter.format(number, result, _fieldPosition);
        return result.toString();
    }
    catch (IllegalArgumentException e) {
        runTimeError(FORMAT_NUMBER_ERR, Double.toString(number), pattern);
        return(EMPTYSTRING);
    }
}
 
Example 14
Source File: BasisLibrary.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
public static String formatNumber(double number, String pattern,
                                  DecimalFormat formatter) {
    // bugzilla fix 12813
    if (formatter == null) {
        formatter = defaultFormatter;
    }
    try {
        StringBuffer result = threadLocalStringBuffer.get();
    result.setLength(0);
        if (pattern != defaultPattern) {
            formatter.applyLocalizedPattern(pattern);
        }
    formatter.format(number, result, _fieldPosition);
        return result.toString();
    }
    catch (IllegalArgumentException e) {
        runTimeError(FORMAT_NUMBER_ERR, Double.toString(number), pattern);
        return(EMPTYSTRING);
    }
}
 
Example 15
Source File: DecimalFormatObjectDescription.java    From ccu-historian with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates an object (<code>DecimalFormat</code>) based on this description.
 *
 * @return The object.
 */
public Object createObject() {
    final DecimalFormat format = (DecimalFormat) super.createObject();
    if (getParameter("pattern") != null) {
        format.applyPattern((String) getParameter("pattern"));
    }
    if (getParameter("localizedPattern") != null) {
        format.applyLocalizedPattern((String) getParameter("localizedPattern"));
    }
    return format;
}
 
Example 16
Source File: LegacyMoneyFormatFactory.java    From template-compiler with Apache License 2.0 5 votes vote down vote up
static NumberFormat create(Locale locale, Currency currency) {
  // The order of these lines matter! For instance, swapping lines 3 and 4 causes 2 decimal places to always be shown.
  DecimalFormat formatter = new DecimalFormat();
  formatter.setCurrency(currency);
  formatter.applyLocalizedPattern(getLocalizedPattern(locale, currency));
  formatter.setMaximumFractionDigits(currency.getDefaultFractionDigits());
  formatter.setDecimalFormatSymbols(getDecimalFormatSymbols(locale, currency));
  return formatter;
}
 
Example 17
Source File: DecimalFormatTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_toLocalizedPattern() {
    DecimalFormat format = new DecimalFormat();
    format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
    format.applyLocalizedPattern("#.#");
    assertEquals("Wrong pattern 1", "#0.#", format.toLocalizedPattern());
    format.applyLocalizedPattern("#.");
    assertEquals("Wrong pattern 2", "#0.", format.toLocalizedPattern());
    format.applyLocalizedPattern("#");
    assertEquals("Wrong pattern 3", "#", format.toLocalizedPattern());
    format.applyLocalizedPattern(".#");
    assertEquals("Wrong pattern 4", "#.0", format.toLocalizedPattern());
}
 
Example 18
Source File: DecimalFormatTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testSetZeroDigitForPattern() {
    DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
    decimalFormatSymbols.setZeroDigit('a');
    DecimalFormat formatter = new DecimalFormat();
    formatter.setDecimalFormatSymbols(decimalFormatSymbols);
    formatter.applyLocalizedPattern("#.aa");
    assertEquals("e.fa", formatter.format(4.50));
}
 
Example 19
Source File: DecimalLocaleConverter.java    From commons-beanutils with Apache License 2.0 5 votes vote down vote up
/**
 * Convert the specified locale-sensitive input object into an output
 * object of the specified type.
 *
 * @param value The input object to be converted
 * @param pattern The pattern is used for the conversion
 * @return The converted value
 *
 * @throws org.apache.commons.beanutils2.ConversionException if conversion
 * cannot be performed successfully
 * @throws ParseException if an error occurs parsing a String to a Number
 */
@Override
protected Object parse(final Object value, final String pattern) throws ParseException {

    if (value instanceof Number) {
        return value;
    }

    // Note that despite the ambiguous "getInstance" name, and despite the
    // fact that objects returned from this method have the same toString
    // representation, each call to getInstance actually returns a new
    // object.
    final DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(locale);

    // if some constructors default pattern to null, it makes only sense
    // to handle null pattern gracefully
    if (pattern != null) {
        if (locPattern) {
            formatter.applyLocalizedPattern(pattern);
        } else {
            formatter.applyPattern(pattern);
        }
    } else {
        log.debug("No pattern provided, using default.");
    }

    return formatter.parse((String) value);
}
 
Example 20
Source File: Location.java    From SuntimesWidget with GNU General Public License v3.0 4 votes vote down vote up
public static DecimalFormat decimalDegreesFormatter()
{
    DecimalFormat formatter = (DecimalFormat)(NumberFormat.getNumberInstance(Locale.US));
    formatter.applyLocalizedPattern(pattern_latLon);
    return formatter;
}