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

The following are Jave code examples for showing how to use setParseBigDecimal() 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: lams   File: NumberFormatter.java   Source Code and License Vote up 6 votes
@Override
public NumberFormat getNumberFormat(Locale locale) {
	NumberFormat format = NumberFormat.getInstance(locale);
	if (!(format instanceof DecimalFormat)) {
		if (this.pattern != null) {
			throw new IllegalStateException("Cannot support pattern for non-DecimalFormat: " + format);
		}
		return format;
	}
	DecimalFormat decimalFormat = (DecimalFormat) format;
	decimalFormat.setParseBigDecimal(true);
	if (this.pattern != null) {
		decimalFormat.applyPattern(this.pattern);
	}
	return decimalFormat;
}
 
Example 4
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 5
Project: oscm   File: PriceConverter.java   Source Code and License Vote up 5 votes
@Override
protected Format getFormat(String pattern, Locale locale) {
    DecimalFormat format = (DecimalFormat) super.getFormat(pattern,
            locale);
    format.setMaximumIntegerDigits(NUMBER_OF_INTEGER_PLACES);
    format.setMaximumFractionDigits(NUMBER_OF_DECIMAL_PLACES);
    // avoid lost precision due to parsing to double:
    format.setParseBigDecimal(true);
    return format;
}
 
Example 6
Project: lams   File: CurrencyFormatter.java   Source Code and License Vote up 5 votes
@Override
protected NumberFormat getNumberFormat(Locale locale) {
	DecimalFormat format = (DecimalFormat) NumberFormat.getCurrencyInstance(locale);
	format.setParseBigDecimal(true);
	format.setMaximumFractionDigits(this.fractionDigits);
	format.setMinimumFractionDigits(this.fractionDigits);
	if (this.roundingMode != null && roundingModeOnDecimalFormat) {
		format.setRoundingMode(this.roundingMode);
	}
	if (this.currency != null) {
		format.setCurrency(this.currency);
	}
	return format;
}
 
Example 7
Project: Hydrograph   File: NumericFunctions.java   Source Code and License Vote up 5 votes
/**
 * Converts {@code inputValue} to bigdecimal value
 *
 * @param inputValue string to be converted to bigdecimal
 * @param scale      number of digits right of decimal point
 * @return converted value of {@code inputValue} to bigdecimal
 * if {@code inputValue} is null return null
 * @throws ParseException if the specified string {@code inputValue}
 *                        cannot be parsed.
 */
public static BigDecimal toBigdecimal(String inputValue, int scale) {
    if (inputValue == null)
        return null;
    DecimalFormat decimalFormat = new DecimalFormat();
    decimalFormat.setParseBigDecimal(true);
    try {
        return ((BigDecimal) decimalFormat.parse(inputValue)).setScale(scale, BigDecimal.ROUND_DOWN);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 8
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 9
Project: mensa-api   File: AbstractDishScraper.java   Source Code and License Vote up 4 votes
AbstractDishScraper() {
    decimalFormat = (DecimalFormat) NumberFormat.getInstance(Locale.GERMANY);
    decimalFormat.setParseBigDecimal(true);
}