Java Float Number Create toFloat(Object o)

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

Description

to Float

License

LGPL

Declaration

public static Float toFloat(Object o) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    public static Float toFloat(Object o) {
        if (o == null)
            return null;

        if (o.getClass() == Float.class)
            return (Float) o;

        if (o.getClass() == Boolean.class)
            return booleanToFloat((Boolean) o);
        if (o.getClass() == Byte.class)
            return (float) ((Byte) o).byteValue();
        if (o.getClass() == Character.class)
            return (float) ((Character) o).charValue();
        if (o.getClass() == Short.class)
            return (float) ((Short) o).shortValue();
        if (o.getClass() == Integer.class)
            return (float) ((Integer) o).intValue();
        if (o.getClass() == Long.class)
            return (float) ((Long) o).longValue();
        if (o.getClass() == Double.class)
            return doubleToFloat((Double) o);
        if (o.getClass() == String.class)
            return stringToFloat((String) o);

        return null;
    }//ww  w. j  av  a 2 s .  c om

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

        if (o.getClass() == Float.class)
            return (Float) o;

        if (o.getClass() == Boolean.class)
            return booleanToFloat((Boolean) o);
        if (o.getClass() == Byte.class)
            return (float) ((Byte) o).byteValue();
        if (o.getClass() == Character.class)
            return (float) ((Character) o).charValue();
        if (o.getClass() == Short.class)
            return (float) ((Short) o).shortValue();
        if (o.getClass() == Integer.class)
            return (float) ((Integer) o).intValue();
        if (o.getClass() == Long.class)
            return (float) ((Long) o).longValue();
        if (o.getClass() == Double.class)
            return doubleToFloat((Double) o, defaultValue);
        if (o.getClass() == String.class)
            return stringToFloat((String) o, defaultValue);

        return defaultValue;
    }

    public static float booleanToFloat(boolean b) {
        return b ? 1f : 0f;
    }

    public static Float doubleToFloat(double d) {
        if (d >= Float.MIN_VALUE && d <= Float.MAX_VALUE) {
            return (float) d;
        }
        return null;
    }

    public static float doubleToFloat(double d, float defaultValue) {
        if (d >= Float.MIN_VALUE && d <= Float.MAX_VALUE) {
            return (float) d;
        }
        return defaultValue;
    }

    public static Float stringToFloat(String s) {
        if (s == null)
            return null;
        try {
            return Float.valueOf(s);
        } catch (NumberFormatException e) {
            return null;
        }
    }

    public static float stringToFloat(String s, float defaultValue) {
        if (s == null)
            return defaultValue;
        try {
            return Float.valueOf(s);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }
}

Related

  1. toFloat(long intPart, long decimalPart, long decimalPlaces)
  2. toFloat(Number value)
  3. toFloat(Object _inStrObj)
  4. toFloat(Object anObj)
  5. toFloat(Object literal)
  6. toFloat(Object o)
  7. toFloat(Object o)
  8. toFloat(Object ob, Float defaultFloat)
  9. toFloat(Object obj)