Java Double Parse tryParseDouble(Object o)

Here you can find the source of tryParseDouble(Object o)

Description

Given an Object that may be null or may be a float or double, this method attempts to convert the value to a Double.

License

Apache License

Parameter

Parameter Description
o the object to try to convert

Return

the converted value, if o is a Float or Double; null otherwise

Declaration

public static Double tryParseDouble(Object o) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from   w  w  w .j av a  2  s .co m
     * Given an <code>Object</code> that may be null or may be a float or double, this
     * method attempts to convert the value to a <code>Double</code>. If successful,
     * the <code>Double</code> value is returned; otherwise, <code>null</code> is returned.
     * NOTE: the [non-null] object is first converted to a string and is trimmed of whitespace.
     * @param o the object to try to convert
     * @return the converted value, if <em>o</em> is a <code>Float or Double</code>; null otherwise
     */
    public static Double tryParseDouble(Object o) {
        if (o == null)
            return null;

        Double retVal = null;
        try {
            retVal = Double.parseDouble(o.toString().trim());
        } catch (NumberFormatException nfe) {
        }

        return retVal;
    }
}

Related

  1. tryParseDouble(Object obj, Double defaultVal)
  2. tryParseDouble(String doubleValue)
  3. tryParseDouble(String value)
  4. tryParseDouble(String value)