public static Double getDouble(Object x) {
        if (x == null) {
            return null;
        }

        if (x instanceof Double) {
            return (Double) x;
        }

        if (x instanceof String) {
            String s = x.toString();
            if (s == null || s.isEmpty()) {
                return null;
            }
        }

        /*
         * This is the last and probably expensive fallback. This should be avoided by
         * only passing in Doubles, Integers, Longs or stuff that can be parsed from it's String
         * representation. You might have to build cached objects that did a safe conversion
         * once for example. There is no way around for the actual values we compare if the
         * user sent them in as non-numerical type.
         */
        return Doubles.tryParse(x.toString());
    }
public static Double getDouble ( Object x ) { if ( x == null ) { return null ; } if ( x instanceof Double ) { return ( Double ) x ; } if ( x instanceof String ) { String s = x . toString ( ) ; if ( s == null || s . isEmpty ( ) ) { return null ; } } return Doubles . tryParse ( x . toString ( ) ) ; }
Convert something to a double in a fast way having a good guess
that it is a double. This is perfect for MongoDB data that *should*
have been stored as doubles already so there is a high probability
of easy converting.

@param x The object to convert to a double
@return Converted object, 0 if empty or something went wrong.