/** * Make an instance of DecimalFormat. * * @param locale The locale * @param pattern The pattern is used for the convertion * @return The format for the locale and pattern * * @throws ConversionException if conversion cannot be performed * successfully * @throws ParseException 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; } }
/** * Make an instance of DecimalFormat. * * @param locale The locale * @param pattern The pattern is used for the convertion * @return The format for the locale and pattern * * @throws ConversionException if conversion cannot be performed * successfully * @throws ParseException 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; } }
public static String formatNumber(double number, String pattern, DecimalFormat formatter) { // bugzilla fix 12813 if (formatter == null) { formatter = defaultFormatter; } try { StringBuffer result = new StringBuffer(); 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); } }
formatter.applyLocalizedPattern(pattern); } else { formatter.applyPattern(pattern);
formatter.applyLocalizedPattern(pattern); } else { formatter.applyPattern(pattern);
formatter.applyLocalizedPattern(patternStr); formatter.applyLocalizedPattern(patternStr); formatter.applyLocalizedPattern(patternStr);
formatter.applyLocalizedPattern(patternStr); formatter.applyLocalizedPattern(patternStr); formatter.applyLocalizedPattern(patternStr);
final DecimalFormatSymbols syms = new DecimalFormatSymbols(); syms.setDecimalSeparator(','); syms.setGroupingSeparator('.'); DecimalFormat myFormatter = new DecimalFormat("", syms); myFormatter.applyLocalizedPattern("###.###,00"); System.out.println(myFormatter.format(1234.12));
/** * Applies a localised format string to the internal <code>DecimalFormat</code> instance. * * @param format * the format string. */ public void setLocalizedFormatString( final String format ) { getDecimalFormat().applyLocalizedPattern( format ); }
public static String formatNumber(double number, String pattern, DecimalFormat formatter) { // bugzilla fix 12813 if (formatter == null) { formatter = defaultFormatter; } try { StringBuffer result = new StringBuffer(); 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); } }
public static String formatNumber(double number, String pattern, DecimalFormat formatter) { // bugzilla fix 12813 if (formatter == null) { formatter = defaultFormatter; } try { StringBuffer result = new StringBuffer(); 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); } }
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); } }
public static String formatNumber(double number, String pattern, DecimalFormat formatter) { // bugzilla fix 12813 if (formatter == null) { formatter = defaultFormatter; } try { StringBuffer result = new StringBuffer(); 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); } }
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); } }
/** * Applies a localised format string to the internal <code>DecimalFormat</code> instance. * * @param format * the format string. */ public void setLocalizedFormatString( final String format ) { getDecimalFormat().applyLocalizedPattern( format ); invalidateCache(); }
/** * 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; } }
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; }
/** * 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; } }
/** Return a new {@link BtcFormat} instance. The object returned will be configured according * to the state of this <code>Builder</code> instance at the time this method is invoked. */ public BtcFormat build() { BtcFormat f = variant.newInstance(this); if (symbol != "" || code != "") { synchronized(f.numberFormat) { DecimalFormatSymbols defaultSigns = f.numberFormat.getDecimalFormatSymbols(); setSymbolAndCode(f.numberFormat, symbol != "" ? symbol : defaultSigns.getCurrencySymbol(), code != "" ? code : defaultSigns.getInternationalCurrencySymbol() ); }} if (localizedPattern != "" || pattern != "") { int places = f.numberFormat.getMinimumFractionDigits(); if (localizedPattern != "") f.numberFormat.applyLocalizedPattern(negify(localizedPattern)); else f.numberFormat.applyPattern(negify(pattern)); f.numberFormat.setMinimumFractionDigits(places); f.numberFormat.setMaximumFractionDigits(places); } return f; }
/** Return a new {@link BtcFormat} instance. The object returned will be configured according * to the state of this <code>Builder</code> instance at the time this method is invoked. */ public BtcFormat build() { BtcFormat f = variant.newInstance(this); if (symbol != "" || code != "") { synchronized(f.numberFormat) { DecimalFormatSymbols defaultSigns = f.numberFormat.getDecimalFormatSymbols(); setSymbolAndCode(f.numberFormat, symbol != "" ? symbol : defaultSigns.getCurrencySymbol(), code != "" ? code : defaultSigns.getInternationalCurrencySymbol() ); }} if (localizedPattern != "" || pattern != "") { int places = f.numberFormat.getMinimumFractionDigits(); if (localizedPattern != "") f.numberFormat.applyLocalizedPattern(negify(localizedPattern)); else f.numberFormat.applyPattern(negify(pattern)); f.numberFormat.setMinimumFractionDigits(places); f.numberFormat.setMaximumFractionDigits(places); } return f; }