Example usage for java.lang Number doubleValue

List of usage examples for java.lang Number doubleValue

Introduction

In this page you can find the example usage for java.lang Number doubleValue.

Prototype

public abstract double doubleValue();

Source Link

Document

Returns the value of the specified number as a double .

Usage

From source file:Main.java

/**
 * This method converts a given number into a target class. This method does not change the value (except when
 * explicitly casting to a more general type, e.g. from double to int), just the internal type representation. While
 * this is unnecessary while using normal java code, reflection based access to method parameters is a bit more
 * difficult. As far as possible, this method will prevent the ArgumentMismatch error when passing numbers as
 * parameters./*from w  ww .  j  a  v a  2s.c  om*/
 * <p/>
 * If the value can not be converted to the given target class, it will be returned unchanged.
 *
 * @param targetClass Class to which the number should be converted, if possible.
 * @param value       Number value to convert.
 * @return 'value' converted to an instance of 'targetClass'.
 */
public static Object convertNumber(final Class targetClass, final Number value) {
    if (targetClass.equals(Double.class) || targetClass.equals(Double.TYPE))
        return value.doubleValue();
    if (targetClass.equals(Integer.class) || targetClass.equals(Integer.TYPE))
        return value.intValue();
    if (targetClass.equals(Long.class) || targetClass.equals(Long.TYPE))
        return value.longValue();
    if (targetClass.equals(Short.class) || targetClass.equals(Short.TYPE))
        return value.shortValue();
    if (targetClass.equals(Byte.class) || targetClass.equals(Byte.TYPE))
        return value.byteValue();
    if (targetClass.equals(Character.class) || targetClass.equals(Character.TYPE))
        return value.intValue();
    if (targetClass.equals(Float.class) || targetClass.equals(Float.TYPE))
        return value.floatValue();
    return value;
}

From source file:org.pentaho.plugin.jfreereport.reportcharts.LogCategoryItemLabelGenerator.java

public static String formatValue(final Number number) {
    if (number == null) {
        return null;
    }/*from www.j  a  v a  2  s  .c o  m*/

    final double rawValue = number.doubleValue();
    final double value = Math.abs(rawValue);

    if (value < 1000.0) {
        return NumberFormat.getNumberInstance().format(rawValue);
    } else if (value < 1000000.0) {
        return NumberFormat.getNumberInstance().format(rawValue / 1000.0) + "K";
    } else if (value < 1000000000.0) {
        return NumberFormat.getNumberInstance().format(rawValue / 1000000.0) + "M";
    } else if (value < 1000000000000.0) {
        return NumberFormat.getNumberInstance().format(rawValue / 1000000000.0) + "B";
    } else {
        return NumberFormat.getNumberInstance().format(rawValue / 1000000000000.0) + "T";
    }
}

From source file:io.coala.time.Rate.java

/**
 * {@link Rate} static factory method/*from  w ww . j a  v  a  2 s  .  com*/
 */
public static Rate of(final Number value, final Unit<Frequency> amount) {
    return of(BigDecimal.valueOf(value.doubleValue()), amount);
}

From source file:mil.jpeojtrs.sca.util.PrimitiveArrayUtils.java

public static double[] convertToDoubleArray(final Object array) {
    if (array == null) {
        return null;
    }//from   w ww .j a v  a2 s .  c  o m
    if (array instanceof double[]) {
        return (double[]) array;
    }
    if (array instanceof Double[]) {
        return ArrayUtils.toPrimitive((Double[]) array);
    }
    final double[] newArray = new double[Array.getLength(array)];
    for (int i = 0; i < newArray.length; i++) {
        final Number val = (Number) Array.get(array, i);
        newArray[i] = val.doubleValue();
    }
    return newArray;
}

From source file:com.github.rinde.rinsim.scenario.generator.PoissonProcessTest.java

static void ascendingOrderTest(List<? extends Number> arrivalTimes) {
    Number prev = 0;
    for (final Number l : arrivalTimes) {
        assertTrue(prev.doubleValue() <= l.doubleValue());
        prev = l;/*w ww .  j av a 2  s . c  o m*/
    }
}

From source file:net.sourceforge.fenixedu.util.InquiriesUtil.java

public static boolean isValidAnswer(final Number answer) {
    return ((answer != null) && (answer.doubleValue() >= 1) && (answer.doubleValue() <= 5));
}

From source file:net.sourceforge.fenixedu.util.InquiriesUtil.java

public static boolean isValidAnswerWithExtraOption(final Number answer) {
    return ((answer != null) && (answer.doubleValue() >= 0) && (answer.doubleValue() <= 5));
}

From source file:net.dontdrinkandroot.utils.lang.math.NumberUtils.java

/**
 * Get the null safe doubleValue of a Number. Defaults to 0.
 * //  w  w w  .  j  a  v a2  s.co  m
 * @param number
 *            The Number to convert.
 * @return The null safe doubleValue. Defaults to 0.
 */
public static double doubleValue(final Number number) {

    if (number == null) {
        return 0d;
    }

    return number.doubleValue();
}

From source file:name.martingeisse.phunky.runtime.variable.TypeConversionUtil.java

/**
 * Converts the specified value to a double-precision floating point value.
 * @param value the original value/*  w ww .  j  a  v a 2  s . c  o  m*/
 * @return the converted value
 */
public static double convertToDouble(Object value) {
    if (value == null) {
        return 0;
    }
    if (value instanceof Number) {
        Number v = (Number) value;
        return v.doubleValue();
    }
    if (value instanceof String) {
        String v = (String) value;
        if (v.isEmpty()) {
            return 0;
        }
        // TODO parse double *prefix* of the string
        try {
            return Double.parseDouble(v);
        } catch (NumberFormatException e) {
            return 1;
        }
    }
    if (value instanceof Boolean) {
        Boolean v = (Boolean) value;
        return (v ? 1 : 0);
    }
    if (value instanceof PhpValueArray) {
        PhpValueArray v = (PhpValueArray) value;
        return (v.isEmpty() ? 0 : 1);
    }
    return 1;
}

From source file:gedi.util.MathUtils.java

/**
 * Throws an exception if n is either a real or to big to be represented by a byte.
 * @param n/*from  w w  w  .ja v a  2 s .  c  o m*/
 * @return
 */
public static byte byteValueExact(Number n) {
    if (n instanceof Byte)
        return n.byteValue();
    double d = n.doubleValue();
    long l = n.longValue();
    if (d == (double) l) {
        if (l >= Byte.MIN_VALUE && l <= Byte.MAX_VALUE)
            return (byte) l;
    }
    throw new NumberFormatException();
}