/** * Delegate to {@link NumberFormat} to parse the value */ private Number parseNumber(String value) { synchronized (numberFormat) { try { return numberFormat.parse(value); } catch (ParseException ex) { String suffix = (numberFormat instanceof DecimalFormat) ? ", use " + ((DecimalFormat) numberFormat).toPattern() : ""; throw new IllegalArgumentException("Number format is invalid: [" + value + "], use " + suffix); } } }
NumberFormat nf = NumberFormat.getCurrencyInstance(); String pattern = ((DecimalFormat) nf).toPattern(); String newPattern = pattern.replace("\u00A4", "").trim(); NumberFormat newFormat = new DecimalFormat(newPattern); System.out.println(newFormat.format(12345.124));
private String decodeDecimalFormat(StringBuffer buffer, Format format) { buffer.append(",number"); if (format.equals(NumberFormat.getNumberInstance(locale))) { // Empty block } else if (format.equals(NumberFormat.getIntegerInstance(locale))) { buffer.append(",integer"); } else if (format.equals(NumberFormat.getCurrencyInstance(locale))) { buffer.append(",currency"); } else if (format.equals(NumberFormat.getPercentInstance(locale))) { buffer.append(",percent"); } else { buffer.append(','); return ((DecimalFormat) format).toPattern(); } return null; }
try { formats[i] = new DecimalFormat(((DecimalFormat) format) .toPattern(), new DecimalFormatSymbols(locale)); } catch (NullPointerException npe){ formats[i] = null;
String pattern = decimalFormat.toPattern(); if (pattern.indexOf(CURRENCY_SYMBOL) >= 0) { StringBuilder buffer = new StringBuilder(pattern.length());
String pattern = decimalFormat.toPattern(); if (pattern.indexOf(PERCENT_SYMBOL) >= 0) { StringBuilder buffer = new StringBuilder(pattern.length());
/** * Convert a String into a <code>Number</code> object. * @param sourceType the source type of the conversion * @param targetType The type to convert the value to * @param value The String date value. * @param format The NumberFormat to parse the String value. * * @return The converted Number object. * @throws ConversionException if the String cannot be converted. */ private Number parse(final Class<?> sourceType, final Class<?> targetType, final String value, final NumberFormat format) { final ParsePosition pos = new ParsePosition(0); final Number parsedNumber = format.parse(value, pos); if (pos.getErrorIndex() >= 0 || pos.getIndex() != value.length() || parsedNumber == null) { String msg = "Error converting from '" + toString(sourceType) + "' to '" + toString(targetType) + "'"; if (format instanceof DecimalFormat) { msg += " using pattern '" + ((DecimalFormat)format).toPattern() + "'"; } if (locale != null) { msg += " for locale=[" + locale + "]"; } if (log().isDebugEnabled()) { log().debug(" " + msg); } throw new ConversionException(msg); } return parsedNumber; }
/** * Convert a String into a <code>Number</code> object. * @param sourceType the source type of the conversion * @param targetType The type to convert the value to * @param value The String date value. * @param format The NumberFormat to parse the String value. * * @return The converted Number object. * @throws ConversionException if the String cannot be converted. */ private Number parse(final Class<?> sourceType, final Class<?> targetType, final String value, final NumberFormat format) { final ParsePosition pos = new ParsePosition(0); final Number parsedNumber = format.parse(value, pos); if (pos.getErrorIndex() >= 0 || pos.getIndex() != value.length() || parsedNumber == null) { String msg = "Error converting from '" + toString(sourceType) + "' to '" + toString(targetType) + "'"; if (format instanceof DecimalFormat) { msg += " using pattern '" + ((DecimalFormat)format).toPattern() + "'"; } if (locale != null) { msg += " for locale=[" + locale + "]"; } if (log().isDebugEnabled()) { log().debug(" " + msg); } throw new ConversionException(msg); } return parsedNumber; }
numberFormat.applyPattern(dFormat.toPattern());
numberFormat.applyPattern(dFormat.toPattern());
if (!conf.hasPath("application.numberFormat")) { nf = Optional.ofNullable(numberFormat) .orElseGet(() -> ((DecimalFormat) DecimalFormat.getInstance(locale)).toPattern()); } else { nf = conf.getString("application.numberFormat");
String fValue = constantOI.getWritableConstantValue().toString(); DecimalFormat dFormat = new DecimalFormat(fValue); numberFormat.applyPattern(dFormat.toPattern()); break; default:
String fValue = constantOI.getWritableConstantValue().toString(); DecimalFormat dFormat = new DecimalFormat(fValue); numberFormat.applyPattern(dFormat.toPattern()); break; default:
data.df.applyPattern( field.getFormat() ); } else { data.df.applyPattern( data.defaultDecimalFormat.toPattern() );
loader.processRow( smi, sdi ); verify( sdi.fifoStream, times( 1 ) ).write( " 001.023".getBytes() ); assertEquals( " #000.000;-#000.000", vmn.getDecimalFormat().toPattern() );
/** * Constructs a parser using the specified set of symbols. * * @param symbols The set of symbols to use. */ public AbstractParser(final Symbols symbols) { this.symbols = symbols; this.numberFormat = (NumberFormat) symbols.numberFormat.clone(); if (SCIENTIFIC_NOTATION && numberFormat instanceof DecimalFormat) { final DecimalFormat numberFormat = (DecimalFormat) this.numberFormat; String pattern = numberFormat.toPattern(); if (pattern.indexOf("E0") < 0) { final int split = pattern.indexOf(';'); if (split >= 0) { pattern = pattern.substring(0, split) + "E0" + pattern.substring(split); } pattern += "E0"; numberFormat.applyPattern(pattern); } } }
/** * Synthesizes a pattern string that represents the current state * of this Format object. * @see #applyPattern */ public String toPattern() { return toPattern( false ); }
private String getPattern(NumberFormat format) { if (format instanceof DecimalFormat) { return ((DecimalFormat) format).toPattern(); } else { logger.warn("Pattern string cannot be determined because NumberFormat is not a DecimalFormat"); return "defaultNumberFormatInstance"; } } }
private String getCurrencyFormatString() { String cf = this.currencyFormat.toPattern(); cf = cf.replace(CURRENCY_SYMBOL_PATTERN, this.currencyFormat.getPositivePrefix()); return cf; }
public static Object formatUsingDefault(final Object object) { if (object instanceof Date) return format(object, FormatController.getController().getDefaultDateTimeFormat().toPattern()); if (object instanceof Number) return format(object, FormatController.getController().getDefaultNumberFormat().toPattern()); return object; }