List of usage examples for java.text DecimalFormat setParseBigDecimal
public void setParseBigDecimal(boolean newValue)
From source file:org.saiku.reporting.backend.component.ReportContentUtil.java
private static Object convert(final ParameterContext context, final ParameterDefinitionEntry parameter, final Class targetType, final Object rawValue) throws ReportProcessingException { if (targetType == null) { throw new NullPointerException(); }/*from ww w.ja v a 2s .co m*/ if (rawValue == null) { return null; } if (targetType.isInstance(rawValue)) { return rawValue; } //TODO: // if (targetType.isAssignableFrom(TableModel.class) && IPentahoResultSet.class.isAssignableFrom(rawValue.getClass())) // { // // wrap IPentahoResultSet to simulate TableModel // return new PentahoTableModel((IPentahoResultSet) rawValue); // } final String valueAsString = String.valueOf(rawValue); if (StringUtils.isEmpty(valueAsString)) { // none of the converters accept empty strings as valid input. So we can return null instead. return null; } if (targetType.equals(Timestamp.class)) { try { final Date date = parseDate(parameter, context, valueAsString); return new Timestamp(date.getTime()); } catch (ParseException pe) { // ignore, we try to parse it as real date now .. } } else if (targetType.equals(Time.class)) { try { final Date date = parseDate(parameter, context, valueAsString); return new Time(date.getTime()); } catch (ParseException pe) { // ignore, we try to parse it as real date now .. } } else if (targetType.equals(java.sql.Date.class)) { try { final Date date = parseDate(parameter, context, valueAsString); return new java.sql.Date(date.getTime()); } catch (ParseException pe) { // ignore, we try to parse it as real date now .. } } else if (targetType.equals(Date.class)) { try { final Date date = parseDate(parameter, context, valueAsString); return new Date(date.getTime()); } catch (ParseException pe) { // ignore, we try to parse it as real date now .. } } final String dataFormat = parameter.getParameterAttribute(ParameterAttributeNames.Core.NAMESPACE, ParameterAttributeNames.Core.DATA_FORMAT, context); if (dataFormat != null) { try { if (Number.class.isAssignableFrom(targetType)) { final DecimalFormat format = new DecimalFormat(dataFormat, new DecimalFormatSymbols(Locale.getDefault())); format.setParseBigDecimal(true); final Number number = format.parse(valueAsString); final String asText = ConverterRegistry.toAttributeValue(number); return ConverterRegistry.toPropertyValue(asText, targetType); } else if (Date.class.isAssignableFrom(targetType)) { final SimpleDateFormat format = new SimpleDateFormat(dataFormat, new DateFormatSymbols(Locale.getDefault())); format.setLenient(false); final Date number = format.parse(valueAsString); final String asText = ConverterRegistry.toAttributeValue(number); return ConverterRegistry.toPropertyValue(asText, targetType); } } catch (Exception e) { // again, ignore it . } } final ValueConverter valueConverter = ConverterRegistry.getInstance().getValueConverter(targetType); if (valueConverter != null) { try { return valueConverter.toPropertyValue(valueAsString); } catch (BeanException e) { throw new ReportProcessingException( "ReportPlugin.unableToConvertParameter " + parameter.getName() + " " + valueAsString); //$NON-NLS-1$ } } return rawValue; }
From source file:org.pentaho.reporting.platform.plugin.ReportContentUtil.java
private static Object convert(final ParameterContext context, final ParameterDefinitionEntry parameter, final Class targetType, final Object rawValue) throws ReportProcessingException { if (targetType == null) { throw new NullPointerException(); }//from w ww . ja v a 2s . co m if (rawValue == null) { return null; } if (targetType.isInstance(rawValue)) { return rawValue; } if (targetType.isAssignableFrom(TableModel.class) && IPentahoResultSet.class.isAssignableFrom(rawValue.getClass())) { // wrap IPentahoResultSet to simulate TableModel return new PentahoTableModel((IPentahoResultSet) rawValue); } final String valueAsString = String.valueOf(rawValue); if (StringUtils.isEmpty(valueAsString)) { // none of the converters accept empty strings as valid input. So we can return null instead. return null; } if (targetType.equals(Timestamp.class)) { try { final Date date = parseDate(parameter, context, valueAsString); return new Timestamp(date.getTime()); } catch (ParseException pe) { // ignore, we try to parse it as real date now .. } } else if (targetType.equals(Time.class)) { try { final Date date = parseDate(parameter, context, valueAsString); return new Time(date.getTime()); } catch (ParseException pe) { // ignore, we try to parse it as real date now .. } } else if (targetType.equals(java.sql.Date.class)) { try { final Date date = parseDate(parameter, context, valueAsString); return new java.sql.Date(date.getTime()); } catch (ParseException pe) { // ignore, we try to parse it as real date now .. } } else if (targetType.equals(Date.class)) { try { final Date date = parseDate(parameter, context, valueAsString); return new Date(date.getTime()); } catch (ParseException pe) { // ignore, we try to parse it as real date now .. } } final String dataFormat = parameter.getParameterAttribute(ParameterAttributeNames.Core.NAMESPACE, ParameterAttributeNames.Core.DATA_FORMAT, context); if (dataFormat != null) { try { if (Number.class.isAssignableFrom(targetType)) { final DecimalFormat format = new DecimalFormat(dataFormat, new DecimalFormatSymbols(LocaleHelper.getLocale())); format.setParseBigDecimal(true); final Number number = format.parse(valueAsString); final String asText = ConverterRegistry.toAttributeValue(number); return ConverterRegistry.toPropertyValue(asText, targetType); } else if (Date.class.isAssignableFrom(targetType)) { final SimpleDateFormat format = new SimpleDateFormat(dataFormat, new DateFormatSymbols(LocaleHelper.getLocale())); format.setLenient(false); final Date number = format.parse(valueAsString); final String asText = ConverterRegistry.toAttributeValue(number); return ConverterRegistry.toPropertyValue(asText, targetType); } } catch (Exception e) { // again, ignore it . } } final ValueConverter valueConverter = ConverterRegistry.getInstance().getValueConverter(targetType); if (valueConverter != null) { try { return valueConverter.toPropertyValue(valueAsString); } catch (BeanException e) { throw new ReportProcessingException(Messages.getInstance() .getString("ReportPlugin.unableToConvertParameter", parameter.getName(), valueAsString)); //$NON-NLS-1$ } } return rawValue; }
From source file:org.crazydog.util.spring.NumberUtils.java
/** * Parse the given text into a number instance of the given target class, * using the given NumberFormat. Trims the input {@code String} * before attempting to parse the number. * * @param text the text to convert * @param targetClass the target class to parse into * @param numberFormat the NumberFormat to use for parsing (if {@code null}, * this method falls back to {@code parseNumber(String, Class)}) * @return the parsed number//from w ww. jav a2 s . c om * @throws IllegalArgumentException if the target class is not supported * (i.e. not a standard Number subclass as included in the JDK) * @see NumberFormat#parse * @see #convertNumberToTargetClass * @see #parseNumber(String, Class) */ public static <T extends Number> T parseNumber(String text, Class<T> targetClass, NumberFormat numberFormat) { if (numberFormat != null) { org.springframework.util.Assert.notNull(text, "Text must not be null"); org.springframework.util.Assert.notNull(targetClass, "Target class must not be null"); DecimalFormat decimalFormat = null; boolean resetBigDecimal = false; if (numberFormat instanceof DecimalFormat) { decimalFormat = (DecimalFormat) numberFormat; if (BigDecimal.class == targetClass && !decimalFormat.isParseBigDecimal()) { decimalFormat.setParseBigDecimal(true); resetBigDecimal = true; } } try { Number number = numberFormat.parse(StringUtils.trimAllWhitespace(text)); return convertNumberToTargetClass(number, targetClass); } catch (ParseException ex) { throw new IllegalArgumentException("Could not parse number: " + ex.getMessage()); } finally { if (resetBigDecimal) { decimalFormat.setParseBigDecimal(false); } } } else { return parseNumber(text, targetClass); } }
From source file:com.prowidesoftware.swift.utils.SwiftFormatUtils.java
/** * Parses a value into a java Number (BigDecimal) using the comma for decimal separator. * @param amount to parse//w ww . ja va 2 s . c o m * @return Number of the parsed amount or <code>null</code> if the number could not be parsed */ public static Number getNumber(final String amount) { Number number = null; if (amount != null) { try { final DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setDecimalSeparator(','); final DecimalFormat df = new DecimalFormat("00.##", symbols); df.setParseBigDecimal(true); number = df.parse(amount); } catch (final ParseException e) { log.log(java.util.logging.Level.WARNING, "Error parsing number", e); } } return number; }
From source file:com.prowidesoftware.swift.utils.SwiftFormatUtils.java
/** * Parses a Number into a SWIFT string number ####,## with truncated zero decimals and mandatory decimal separator. * <ul>/* w ww .j a v a 2s. c om*/ * <li>Example: 1234.00 -> 1234,</li> * <li>Example: 1234 -> 1234,</li> * <li>Example: 1234.56 -> 1234,56</li> * </ul> * @param number to parse * @return Number of the parsed amount or <code>null</code> if the number is null */ public static String getNumber(final Number number) { if (number != null) { final DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setDecimalSeparator(','); final DecimalFormat df = new DecimalFormat("0.##########", symbols); df.setParseBigDecimal(true); df.setDecimalSeparatorAlwaysShown(true); final String formatted = df.format(number); final String result = StringUtils.replaceChars(formatted, '.', ','); return result; } return null; }
From source file:com.trenako.format.CurrencyFormatter.java
@Override protected NumberFormat getNumberFormat(Locale locale) { DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(locale); format.setParseBigDecimal(true); return format; }
From source file:com.intuit.karate.Script.java
private static BigDecimal convertToBigDecimal(Object o) { DecimalFormat df = new DecimalFormat(); df.setParseBigDecimal(true); try {// www . ja va 2s . co m return (BigDecimal) df.parse(o.toString()); } catch (Exception e) { logger.warn("big decimal conversion failed: {}", e.getMessage()); return null; } }
From source file:com.haulmont.chile.core.datatypes.impl.BigDecimalDatatype.java
@Override public BigDecimal parse(String value, Locale locale) throws ParseException { if (StringUtils.isBlank(value)) { return null; }/*from w w w. j a v a 2 s . c o m*/ FormatStrings formatStrings = AppBeans.get(FormatStringsRegistry.class).getFormatStrings(locale); if (formatStrings == null) { return parse(value); } DecimalFormatSymbols formatSymbols = formatStrings.getFormatSymbols(); DecimalFormat format = new DecimalFormat(formatStrings.getDecimalFormat(), formatSymbols); format.setParseBigDecimal(true); return (BigDecimal) parse(value, format); }
From source file:de.jfachwert.pruefung.NumberValidator.java
/** * Normalisiert einen String, sodass er zur Generierung einer Zahl * herangezogen werden kann./*from w w w.j a v a 2 s .c o m*/ * * @param value z.B. "1,234.5" * @return normalisiert, z.B. "1234.5" */ public String normalize(String value) { if (!value.matches("[\\d,.]+([eE]\\d+)?")) { throw new InvalidValueException(value, NUMBER); } Locale locale = guessLocale(value); DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(locale); df.setParseBigDecimal(true); try { return df.parse(value).toString(); } catch (ParseException ex) { throw new InvalidValueException(value, NUMBER, ex); } }
From source file:org.opensingular.form.wicket.mapper.DecimalMapper.java
private String formatDecimal(BigDecimal bigDecimal, boolean groupingUsed) { DecimalFormat nf = (DecimalFormat) DecimalFormat.getInstance(new Locale("pt", "BR")); nf.setParseBigDecimal(true); nf.setGroupingUsed(groupingUsed);// w ww .ja v a 2 s. c om nf.setMinimumFractionDigits(0); nf.setMaximumFractionDigits(bigDecimal.scale()); return nf.format(bigDecimal); }