Exemple #1
0
  public static float toFloat(Object obj) {
    if (obj.getClass() == float.class) {
      return (Float) obj;
    }

    try {
      if (obj instanceof Float) {
        return (Float) obj;
      } else if (obj instanceof Number) {
        return ((Number) obj).floatValue();
      } else if (obj instanceof CharSequence) {
        try {
          return Float.parseFloat(Str.toString(obj));
        } catch (Exception ex) {
          die(String.format("Unable to convert %s to a float", obj.getClass()));
          return Float.MIN_VALUE;
        }
      } else {
      }
    } catch (Exception ex) {

      log.warning(
          String.format(
              "unable to convert to float and there was an exception %s", ex.getMessage()));
    }

    die(String.format("Unable to convert %s to a float", obj.getClass()));
    return Float.MIN_VALUE;
  }
Exemple #2
0
  public static double toDouble(Object obj) {

    try {
      if (obj instanceof Double) {
        return (Double) obj;
      } else if (obj instanceof Number) {
        return ((Number) obj).doubleValue();
      } else if (obj instanceof CharSequence) {
        try {
          return Double.parseDouble(((CharSequence) obj).toString());
        } catch (Exception ex) {
          die(String.format("Unable to convert %s to a double", obj.getClass()));
          return Double.MIN_VALUE;
        }
      } else {
      }
    } catch (Exception ex) {
      log.warning(
          String.format(
              "unable to convert to double and there was an exception %s", ex.getMessage()));
    }

    die(String.format("Unable to convert %s to a double", obj.getClass()));
    return Double.MIN_VALUE; // die throws an exception
  }