Java Code Examples for java.text.DecimalFormat.parse()

The following are Jave code examples for showing how to use parse() of the java.text.DecimalFormat class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: queryfy   File: NumberVar.java   Source Code and License Vote up 6 votes
private void build() {
    try {

        if (typeQualifier != null) {
            buildQualifiedNumber();
            return;
        }

        Number value;
        if (number.contains(".")) {
            DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
            decimalFormat.setParseBigDecimal(true);

            int decSymbolIndex = number.lastIndexOf(".");
            if (decSymbolIndex > -1) {
                int precision = number.substring(decSymbolIndex, number.length() - 1).length();
                decimalFormat.setMaximumFractionDigits(precision);
            }

            value = decimalFormat.parse(number);
        } else {
            value = NumberFormat.getInstance(locale).parse(number);
        }

        set(value);
    } catch (ParseException e) {
        throw new RuntimeException(String.format("Invalid number '%s'", number));
    }
}
 
Example 2
Project: karate   File: Script.java   Source Code and License Vote up 6 votes
private static BigDecimal convertToBigDecimal(Object o) {
    DecimalFormat df = new DecimalFormat();
    df.setParseBigDecimal(true);
    try {
        return (BigDecimal) df.parse(o.toString());
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 3
Project: Reer   File: LocaleSafeDecimalFormat.java   Source Code and License Vote up 5 votes
/**
 * Regardless of the default locale, comma ('.') is used as decimal separator
 *
 * @param source
 * @return
 * @throws ParseException
 */
public BigDecimal parse(String source) throws ParseException {
    DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    symbols.setDecimalSeparator('.');
    DecimalFormat format = new DecimalFormat("#.#", symbols);
    format.setParseBigDecimal(true);
    return (BigDecimal) format.parse(source);
}
 
Example 4
Project: NotifyTools   File: DecimalLocaleConverter.java   Source Code and License Vote up 5 votes
/**
 * 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 convertion
 * @return The converted value
 *
 * @throws org.apache.commons.beanutils.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) DecimalFormat.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 5
Project: metanome-algorithms   File: DataTypes.java   Source Code and License Vote up 5 votes
public static boolean isDecimal(String input) {
  try {
    DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    symbols.setGroupingSeparator(',');
    symbols.setDecimalSeparator('.');
    String pattern = "#,##0.0#";
    DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols);
    BigDecimal bigDecimal = (BigDecimal) decimalFormat.parse(input);
    return true;
  } catch (Exception ex) {
    return false;
  }
}
 
Example 6
Project: oscm   File: DurationValidation.java   Source Code and License Vote up 5 votes
/**
 * Parses the given value considering the current locale and returns the
 * number representation of the string. The representation is based on the
 * {@link #DURATION_FORMAT}.
 * 
 * @param valueToCheck
 *            The string to be parsed. Must not be <code>null</code>.
 * @return The number representation of the parameter.
 * @throws ParseException
 */
public static Number getParsedDuration(FacesContext facesContext,
        String valueToCheck) {
    Locale locale = LocaleHandler.getLocaleFromString(BaseBean
            .getUserFromSession(facesContext).getLocale());
    DecimalFormatSymbols dfs = new DecimalFormatSymbols(locale);
    DecimalFormat df = new DecimalFormat(DURATION_FORMAT, dfs);
    df.setGroupingUsed(true);
    try {
        return df.parse(valueToCheck);
    } catch (ParseException e) {
        return null;
    }
}
 
Example 7
Project: oscm   File: DurationParameterValidator.java   Source Code and License Vote up 5 votes
/**
 * Taken from org.oscm.ui.common.DurationValidation
 * 
 * @param valueToCheck
 * @return
 */
private Number getParsedDuration(String valueToCheck) {
    DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.getDefault());
    DecimalFormat df = new DecimalFormat(DURATION_FORMAT, dfs);
    df.setGroupingUsed(true);
    try {
        return df.parse(valueToCheck);
    } catch (ParseException e) {
        return null;
    }
}
 
Example 8
Project: java-course   File: ValueWrapper.java   Source Code and License Vote up 5 votes
/**
 * Returns parsed number.
 * @param number String number representation.
 * @return Parsed number.
 * @throws ParseException Unable to parse given string.
 */
private Number getDecimalNumber(String number) throws ParseException {
	DecimalFormat format = new DecimalFormat();
	DecimalFormatSymbols custom=new DecimalFormatSymbols();
	custom.setDecimalSeparator('.');
	format.setDecimalFormatSymbols(custom);
	return format.parse(number);
}
 
Example 9
Project: jspoon   File: BigDecimalFormatTest.java   Source Code and License Vote up 5 votes
@Test
public void amount() throws ParseException {
    Money money = createObjectFromHtml(Money.class);
    DecimalFormat format = new DecimalFormat("0,000.00");
    format.setParseBigDecimal(true);
    BigDecimal expected = (BigDecimal) format.parse("50,000.00");
    assertEquals(money.amount, expected);
}
 
Example 10
Project: GestureFX   File: LenaSample.java   Source Code and License Vote up 5 votes
private static TextFormatter<String> createDecimalFormatter() {
	DecimalFormat format = new DecimalFormat("#.0");
	format.setNegativePrefix("-");
	return new TextFormatter<>(c -> {
		if (c.getControlNewText().isEmpty()) return c;
		ParsePosition pos = new ParsePosition(0);
		Number result = format.parse(c.getControlNewText(), pos);
		if (result == null || pos.getIndex() < c.getControlNewText().length()) {
			return null;
		} else return c;
	});
}
 
Example 11
Project: morpheus-core   File: Parser.java   Source Code and License Vote up 5 votes
/**
 * Returns a newly created Parser for Double
 * @param pattern   the decimal format pattern
 * @param multiplier    the multiplier to apply
 * @return  newly created Parser
 */
public static Parser<Double> ofDouble(String pattern, int multiplier) {
    final DecimalFormat decimalFormat = createDecimalFormat(pattern, multiplier);
    return new ParserOfDouble(defaultNullCheck, value -> {
        try {
            return decimalFormat.parse(value);
        } catch (Exception ex) {
            throw new FormatException("Failed to parse value into double: " + value, ex);
        }
    });
}
 
Example 12
Project: java-course   File: ValueWrapper.java   Source Code and License Vote up 3 votes
/**
 * Returns parsed number.
 * 
 * @param number
 *            String number representation.
 * @return Parsed number.
 * @throws ParseException
 *             Unable to parse given string.
 */
private Number getDecimalNumber(String number) throws ParseException {
    DecimalFormat format = new DecimalFormat();
    DecimalFormatSymbols custom = new DecimalFormatSymbols();
    custom.setDecimalSeparator('.');
    format.setDecimalFormatSymbols(custom);
    return format.parse(number);
}