Java Double Number Create toDouble(Object _value, double _default)

Here you can find the source of toDouble(Object _value, double _default)

Description

to Double

License

Open Source License

Declaration

public static double toDouble(Object _value, double _default) 

Method Source Code

//package com.java2s;
/* // w w w  . j av a2 s .  c  om
 *  Copyright (C) 2011,2012 AW2.0 Ltd
 *
 *  org.aw20 is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  Free Software Foundation,version 3.
 *  
 *  OpenBD is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with org.aw20.  If not, see http://www.gnu.org/licenses/
 *  
 *  Additional permission under GNU GPL version 3 section 7
 *  
 *  If you modify this Program, or any covered work, by linking or combining 
 *  it with any of the JARS listed in the README.txt (or a modified version of 
 *  (that library), containing parts covered by the terms of that JAR, the 
 *  licensors of this Program grant you additional permission to convey the 
 *  resulting work. 
 *  
 *  $Id: StringUtil.java 3517 2012-12-19 16:33:25Z andy $
 */

public class Main {
    public static double toDouble(Object _value, double _default) {
        if (_value == null)
            return _default;
        else if (_value instanceof Integer)
            return ((Integer) _value).doubleValue();
        else if (_value instanceof Long)
            return ((Long) _value).doubleValue();
        else if (_value instanceof Double)
            return (Double) _value;
        else if (_value instanceof Float)
            return ((Float) _value).doubleValue();
        else
            return toDouble(_value.toString(), _default);
    }

    public static double toDouble(String _value, double _default) {
        if (_value == null || _value.length() == 0)
            return _default;
        else {
            try {
                return Double.parseDouble(_value);
            } catch (Exception e) {
                return _default;
            }
        }
    }

    /**
     * Tries to cast Object to String. If object is null, it will return null.
     * 
     * @param _o
     * @return String value of _o
     */
    public static String toString(Object _o) {
        return toString(_o, null);
    }

    /**
     * Tries to cast Object to String. If object is null, it will return the default value supplied..
     * 
     * @param _o
     * @param _default
     * @return String value of _o
     */
    public static String toString(Object _o, String _default) {
        if (_o == null) {
            return _default;
        } else {
            try {
                return (String) _o;
            } catch (ClassCastException e) {
                return _default;
            }
        }
    }
}

Related

  1. toDouble(Number n)
  2. toDouble(Number number)
  3. toDouble(Number value)
  4. toDouble(Number value)
  5. toDouble(Object _inStrObj)
  6. toDouble(Object argSource)
  7. toDouble(Object literal)
  8. toDouble(Object number)
  9. toDouble(Object number)