Example usage for java.text DecimalFormat parseObject

List of usage examples for java.text DecimalFormat parseObject

Introduction

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

Prototype

public Object parseObject(String source) throws ParseException 

Source Link

Document

Parses text from the beginning of the given string to produce an object.

Usage

From source file:net.pms.util.Rational.java

/**
 * Parses the specified {@link String} and returns a {@link BigDecimal}
 * using the specified {@link DecimalFormat}. {@code value} is expected to
 * be without leading or trailing whitespace. If {@code value} is blank,
 * {@code null} will be returned./*from  ww  w . j  a v a2s.co  m*/
 *
 * @param value the {@link String} to parse.
 * @param decimalFormat the {@link DecimalFormat} to use when parsing.
 * @return The resulting {@link BigDecimal}.
 * @throws NumberFormatException If {@code value} cannot be parsed.
 */
@Nullable
public static BigDecimal parseBigDecimal(@Nullable String value, @Nullable DecimalFormat decimalFormat) {
    if (StringUtils.isBlank(value)) {
        return null;
    }

    if (decimalFormat != null) {
        decimalFormat.setParseBigDecimal(true);
        try {
            return (BigDecimal) decimalFormat.parseObject(value);
        } catch (ParseException e) {
            throw new NumberFormatException("Unable to parse \"" + value + "\": " + e.getMessage());
        }
    }
    return new BigDecimal(value);
}