Java Double Number Create toDouble(Object o)

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

Description

to Double

License

LGPL

Declaration

public static Double toDouble(Object o) 

Method Source Code

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

public class Main {
    public static Double toDouble(Object o) {
        if (o == null)
            return null;

        if (o.getClass() == Double.class)
            return (Double) o;

        if (o.getClass() == Boolean.class)
            return booleanToDouble((Boolean) o);
        if (o.getClass() == Byte.class)
            return (double) ((Byte) o).byteValue();
        if (o.getClass() == Character.class)
            return (double) ((Character) o).charValue();
        if (o.getClass() == Short.class)
            return (double) ((Short) o).shortValue();
        if (o.getClass() == Integer.class)
            return (double) ((Integer) o).intValue();
        if (o.getClass() == Long.class)
            return (double) ((Long) o).longValue();
        if (o.getClass() == Float.class)
            return (double) ((Float) o).floatValue();
        if (o.getClass() == String.class)
            return stringToDouble((String) o);

        return null;
    }/*from w ww  .  j a  va  2s . c o m*/

    public static double toDouble(Object o, double defaultValue) {
        if (o == null)
            return defaultValue;

        if (o.getClass() == Double.class)
            return (Double) o;

        if (o.getClass() == Boolean.class)
            return booleanToDouble((Boolean) o);
        if (o.getClass() == Byte.class)
            return (double) ((Byte) o).byteValue();
        if (o.getClass() == Character.class)
            return (double) ((Character) o).charValue();
        if (o.getClass() == Short.class)
            return (double) ((Short) o).shortValue();
        if (o.getClass() == Integer.class)
            return (double) ((Integer) o).intValue();
        if (o.getClass() == Long.class)
            return (double) ((Long) o).longValue();
        if (o.getClass() == Float.class)
            return (double) ((Float) o).floatValue();
        if (o.getClass() == String.class)
            return stringToDouble((String) o, defaultValue);

        return defaultValue;
    }

    public static double booleanToDouble(boolean b) {
        return b ? 1. : 0.;
    }

    public static Double stringToDouble(String s) {
        if (s == null)
            return null;
        try {
            return Double.valueOf(s);
        } catch (NumberFormatException e) {
            return null;
        }
    }

    public static double stringToDouble(String s, double defaultValue) {
        if (s == null)
            return defaultValue;
        try {
            return Double.valueOf(s);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }
}

Related

  1. toDouble(Object o)
  2. toDouble(Object o)
  3. toDouble(Object o)
  4. toDouble(Object o)
  5. toDouble(Object o)
  6. toDouble(Object ob, Double defaultDouble)
  7. toDouble(Object obj)
  8. toDouble(Object obj)
  9. toDouble(Object obj)