Example usage for java.text DecimalFormat isParseBigDecimal

List of usage examples for java.text DecimalFormat isParseBigDecimal

Introduction

In this page you can find the example usage for java.text DecimalFormat isParseBigDecimal.

Prototype

public boolean isParseBigDecimal() 

Source Link

Document

Returns whether the #parse(java.lang.String,java.text.ParsePosition) method returns BigDecimal .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DecimalFormat format = new DecimalFormat();

    System.out.println(format.isParseBigDecimal());

}

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//  w w w. java  2  s . c o m
 * @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);
    }
}