Parse a String to its Double representation. - Java java.lang

Java examples for java.lang:double

Description

Parse a String to its Double representation.

Demo Code



public class Main{
    /**/*from  w  w w.java2  s.  c o  m*/
     * Parse a String to its Double representation. If the operation not succeeded returns null.
     *
     * @param rawValue The String to parse.
     * @return The Double representation of the String. If the operation not succeeded returns null.
     */
    public static Double tryParse(String rawValue, Double defaultValue) {
        if (StringUtils.isBlank(rawValue)) {
            // No Parseable.
            return defaultValue;
        }

        try {
            // Try to parse.
            return Double.parseDouble(rawValue);
        } catch (NumberFormatException ex) {
            // No Parseable.
            return defaultValue;
        }
    }
    public static Double tryParse(String rawValue) {
        return tryParse(rawValue, null);
    }
}

Related Tutorials