Java Double to doubleToBasicType(double d, Class clazz)

Here you can find the source of doubleToBasicType(double d, Class clazz)

Description

double To Basic Type

License

LGPL

Declaration

@SuppressWarnings("unchecked")
    public static <T> T doubleToBasicType(double d, Class<T> clazz) 

Method Source Code

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

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T doubleToBasicType(double d, Class<T> clazz) {
        if (clazz.equals(Boolean.class))
            return (T) Boolean.valueOf(doubleToBoolean(d));
        if (clazz.equals(Byte.class))
            return (T) doubleToByte(d);
        if (clazz.equals(Character.class))
            return (T) doubleToCharacter(d);
        if (clazz.equals(Short.class))
            return (T) doubleToShort(d);
        if (clazz.equals(Integer.class))
            return (T) doubleToInteger(d);
        if (clazz.equals(Long.class))
            return (T) doubleToLong(d);
        if (clazz.equals(Float.class))
            return (T) doubleToFloat(d);
        if (clazz.equals(Double.class))
            return (T) Double.valueOf(d);
        if (clazz.equals(String.class))
            return (T) String.valueOf(d);

        return null;
    }//  w w  w.j  ava  2  s.  c om

    public static boolean doubleToBoolean(double d) {
        return d != 0 ? true : false;
    }

    public static Byte doubleToByte(double d) {
        if (d >= Byte.MIN_VALUE && d <= Byte.MAX_VALUE) {
            return (byte) d;
        }
        return null;
    }

    public static byte doubleToByte(double d, byte defaultValue) {
        if (d >= Byte.MIN_VALUE && d <= Byte.MAX_VALUE) {
            return (byte) d;
        }
        return defaultValue;
    }

    public static Character doubleToCharacter(double d) {
        if (d >= Character.MIN_VALUE && d <= Character.MAX_VALUE) {
            return (char) d;
        }
        return null;
    }

    public static Short doubleToShort(double d) {
        if (d >= Short.MIN_VALUE && d <= Short.MAX_VALUE) {
            return (short) d;
        }
        return null;
    }

    public static short doubleToShort(double d, short defaultValue) {
        if (d >= Short.MIN_VALUE && d <= Short.MAX_VALUE) {
            return (short) d;
        }
        return defaultValue;
    }

    public static Integer doubleToInteger(double d) {
        if (d >= Integer.MIN_VALUE && d <= Integer.MAX_VALUE) {
            return (int) d;
        }
        return null;
    }

    public static Long doubleToLong(double d) {
        if (d >= Long.MIN_VALUE && d <= Long.MAX_VALUE) {
            return (long) d;
        }
        return null;
    }

    public static long doubleToLong(double d, long defaultValue) {
        if (d >= Long.MIN_VALUE && d <= Long.MAX_VALUE) {
            return (long) d;
        }
        return defaultValue;
    }

    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;
    }
}

Related

  1. doubleTo2DArray(double[] array)
  2. doubleToBinaryString(double value)
  3. doubleToBits(double in, double min, double max, int numBits, int splits)
  4. doubleToBits(double num)
  5. doubleToChar(double d, char defaultValue)