Java Double Number Create toDouble(Object number)

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

Description

to Double

License

Open Source License

Declaration

public static double toDouble(Object number) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static double toDouble(Object number) {
        if (number == null) {
            return 0.0D;
        } else if (number instanceof Number) {
            return ((Number) number).doubleValue();
        } else if (number instanceof String) {
            String str = (String) number;
            return isNumeric(str) > 0 ? Double.valueOf(str) : 0.0D;
        } else {/*from  w  w  w  .  j  av a  2s .co  m*/
            return 0.0D;
        }
    }

    public static int isNumeric(String str) {
        if (str == null) {
            return 0;
        } else {
            boolean isdouble = false;
            boolean hasE = false;
            int i = str.length();

            while (true) {
                while (true) {
                    char c;
                    do {
                        --i;
                        if (i < 0) {
                            if (isdouble) {
                                return 2;
                            }

                            return 1;
                        }

                        c = str.charAt(i);
                    } while (i == 0 && c == 45);

                    if (c == 46) {
                        if (isdouble) {
                            return 0;
                        }

                        isdouble = true;
                    } else if (c != 69 && c != 101) {
                        if (!Character.isDigit(str.charAt(i))) {
                            return 0;
                        }
                    } else {
                        if (hasE) {
                            return 0;
                        }

                        hasE = true;
                    }
                }
            }
        }
    }
}

Related

  1. toDouble(Number value)
  2. toDouble(Object _inStrObj)
  3. toDouble(Object _value, double _default)
  4. toDouble(Object argSource)
  5. toDouble(Object literal)
  6. toDouble(Object number)
  7. toDouble(Object o)
  8. toDouble(Object o)
  9. toDouble(Object o)