Java Integer Create toInt(Object obj)

Here you can find the source of toInt(Object obj)

Description

to Int

License

Open Source License

Declaration

public static int toInt(Object obj) 

Method Source Code

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

public class Main {
    public static int toInt(Object obj) {
        return toInt(obj, 0);
    }/*  w w  w.java 2s .  c  o  m*/

    public static int toInt(Object obj, int defaultValue) {
        if (obj == null) {
            return defaultValue;
        }

        if (obj instanceof Number) {
            Number number = (Number) obj;
            return number.intValue();
        }
        String value = toString(obj);
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {
        }
        return defaultValue;
    }

    public static String toString(Object value) {
        if (value == null) {
            return "";
        }
        return value.toString().trim();
    }
}

Related

  1. toInt(Object obj)
  2. toInt(Object obj)
  3. toInt(Object obj)
  4. toInt(Object obj)
  5. toInt(Object obj)
  6. toInt(Object object)
  7. toInt(Object object, int defaultValue)
  8. toInt(Object objValue)
  9. toInt(Object property, int defaultValue)