Java Float to floatToBasicType(float f, Class clazz)

Here you can find the source of floatToBasicType(float f, Class clazz)

Description

float To Basic Type

License

LGPL

Declaration

@SuppressWarnings("unchecked")
    public static <T> T floatToBasicType(float f, Class<T> clazz) 

Method Source Code

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

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T floatToBasicType(float f, Class<T> clazz) {
        if (clazz.equals(Boolean.class))
            return (T) Boolean.valueOf(floatToBoolean(f));
        if (clazz.equals(Byte.class))
            return (T) floatToByte(f);
        if (clazz.equals(Character.class))
            return (T) floatToCharacter(f);
        if (clazz.equals(Short.class))
            return (T) floatToShort(f);
        if (clazz.equals(Integer.class))
            return (T) floatToInteger(f);
        if (clazz.equals(Long.class))
            return (T) floatToLong(f);
        if (clazz.equals(Float.class))
            return (T) Float.valueOf(f);
        if (clazz.equals(Double.class))
            return (T) Double.valueOf(f);
        if (clazz.equals(String.class))
            return (T) String.valueOf(f);

        return null;
    }/*w w  w.  j a  va2s .  c  om*/

    public static boolean floatToBoolean(float f) {
        return f != 0 ? true : false;
    }

    public static Byte floatToByte(float f) {
        if (f >= Byte.MIN_VALUE && f <= Byte.MAX_VALUE) {
            return (byte) f;
        }
        return null;
    }

    public static byte floatToByte(float f, byte defaultValue) {
        if (f >= Byte.MIN_VALUE && f <= Byte.MAX_VALUE) {
            return (byte) f;
        }
        return defaultValue;
    }

    public static Character floatToCharacter(float f) {
        if (f >= Character.MIN_VALUE && f <= Character.MAX_VALUE) {
            return (char) f;
        }
        return null;
    }

    public static Short floatToShort(float f) {
        if (f >= Short.MIN_VALUE && f <= Short.MAX_VALUE) {
            return (short) f;
        }
        return null;
    }

    public static short floatToShort(float f, short defaultValue) {
        if (f >= Short.MIN_VALUE && f <= Short.MAX_VALUE) {
            return (short) f;
        }
        return defaultValue;
    }

    public static Integer floatToInteger(float f) {
        if (f >= Integer.MIN_VALUE && f <= Integer.MAX_VALUE) {
            return (int) f;
        }
        return null;
    }

    public static Long floatToLong(float f) {
        if (f >= Long.MIN_VALUE && f <= Long.MAX_VALUE) {
            return (long) f;
        }
        return null;
    }

    public static long floatToLong(float f, long defaultValue) {
        if (f >= Long.MIN_VALUE && f <= Long.MAX_VALUE) {
            return (long) f;
        }
        return defaultValue;
    }
}

Related

  1. floatToBinaryStringUnsigned(float value)
  2. floatToChar(float[] values)
  3. floatToHalf(float f)
  4. FloatToInt(double x)