Java Float Number Create toFloat(Object obj)

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

Description

to Float

License

Open Source License

Declaration

public static float toFloat(Object obj) 

Method Source Code

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

public class Main {
    public static float toFloat(Object obj) {
        return toFloat(obj, 0);
    }// ww  w .j av a2 s  . c  om

    public static float toFloat(Object obj, float defaultValue) {
        if (obj == null) {
            return defaultValue;
        }

        if (obj instanceof Number) {
            Number number = (Number) obj;
            return number.floatValue();
        }
        String value = toString(obj);
        try {
            return Float.parseFloat(value);
        } catch (Exception e) {
        }
        return defaultValue;
    }

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

Related

  1. toFloat(Object o)
  2. toFloat(Object o)
  3. toFloat(Object o)
  4. toFloat(Object ob, Float defaultFloat)
  5. toFloat(Object obj)
  6. toFloat(Object obj)
  7. toFloat(Object obj)
  8. toFloat(Object obj, String pattern)
  9. toFloat(Object rawColor)