Java Long Number Create toLong(Object number)

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

Description

to Long

License

Open Source License

Declaration

public static long toLong(Object number) 

Method Source Code

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

public class Main {
    public static long toLong(Object number) {
        if (number == null) {
            return 0L;
        } else if (number instanceof Number) {
            return ((Number) number).longValue();
        } else if (number instanceof String) {
            String str = (String) number;
            int isNumber = isNumeric(str);
            return isNumber == 1 ? Long.parseLong(str) : (isNumber == 2 ? Double.valueOf(str).longValue() : 0L);
        } else {//www  .  jav  a 2 s  .c  o m
            return 0L;
        }
    }

    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. toLong(Object _value, long _default)
  2. toLong(Object cell)
  3. toLong(Object expectInt)
  4. toLong(Object num)
  5. toLong(Object num, long defValue)
  6. toLong(Object o)
  7. toLong(Object o)
  8. toLong(Object o)
  9. toLong(Object ob, Long defaultLong)