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:org.jfree.data.statistics.BoxAndWhiskerCalculator.java

/**
 * Calculates the statistics required for a {@link BoxAndWhiskerItem}
 * from a list of <code>Number</code> objects.  Any items in the list
 * that are <code>null</code>, not an instance of <code>Number</code>, or
 * equivalent to <code>Double.NaN</code>, will be ignored.
 *
 * @param values  a list of numbers (a <code>null</code> list is not
 *                permitted)./*from w  ww. j  av  a  2s. c o  m*/
 * @param stripNullAndNaNItems  a flag that controls the handling of null
 *     and NaN items.
 *
 * @return A box-and-whisker item.
 *
 * @since 1.0.3
 */
public static BoxAndWhiskerItem calculateBoxAndWhiskerStatistics(List values, boolean stripNullAndNaNItems) {

    ParamChecks.nullNotPermitted(values, "values");

    List vlist;
    if (stripNullAndNaNItems) {
        vlist = new ArrayList(values.size());
        Iterator iterator = values.listIterator();
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            if (obj instanceof Number) {
                Number n = (Number) obj;
                double v = n.doubleValue();
                if (!Double.isNaN(v)) {
                    vlist.add(n);
                }
            }
        }
    } else {
        vlist = values;
    }
    Collections.sort(vlist);

    double mean = Statistics.calculateMean(vlist, false);
    double median = Statistics.calculateMedian(vlist, false);
    double q1 = calculateQ1(vlist);
    double q3 = calculateQ3(vlist);

    double interQuartileRange = q3 - q1;

    double upperOutlierThreshold = q3 + (interQuartileRange * 1.5);
    double lowerOutlierThreshold = q1 - (interQuartileRange * 1.5);

    double upperFaroutThreshold = q3 + (interQuartileRange * 2.0);
    double lowerFaroutThreshold = q1 - (interQuartileRange * 2.0);

    double minRegularValue = Double.POSITIVE_INFINITY;
    double maxRegularValue = Double.NEGATIVE_INFINITY;
    double minOutlier = Double.POSITIVE_INFINITY;
    double maxOutlier = Double.NEGATIVE_INFINITY;
    List outliers = new ArrayList();

    Iterator iterator = vlist.iterator();
    while (iterator.hasNext()) {
        Number number = (Number) iterator.next();
        double value = number.doubleValue();
        if (value > upperOutlierThreshold) {
            outliers.add(number);
            if (value > maxOutlier && value <= upperFaroutThreshold) {
                maxOutlier = value;
            }
        } else if (value < lowerOutlierThreshold) {
            outliers.add(number);
            if (value < minOutlier && value >= lowerFaroutThreshold) {
                minOutlier = value;
            }
        } else {
            minRegularValue = Math.min(minRegularValue, value);
            maxRegularValue = Math.max(maxRegularValue, value);
        }
        minOutlier = Math.min(minOutlier, minRegularValue);
        maxOutlier = Math.max(maxOutlier, maxRegularValue);
    }

    return new BoxAndWhiskerItem(new Double(mean), new Double(median), new Double(q1), new Double(q3),
            new Double(minRegularValue), new Double(maxRegularValue), new Double(minOutlier),
            new Double(maxOutlier), outliers);

}

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/*www  .  j a v a 2s  .  c om*/
 * @return
 */
public static short shortValueExact(Number n) {
    if (n instanceof Short || n instanceof Byte)
        return n.shortValue();
    double d = n.doubleValue();
    long l = n.longValue();
    if (d == (double) l) {
        if (l >= Short.MIN_VALUE && l <= Short.MAX_VALUE)
            return (short) l;
    }
    throw new NumberFormatException();
}

From source file:edu.harvard.iq.dataverse.util.SumStatCalculator.java

public static double[] calculateSummaryStatistics(Number[] x) {
    logger.fine("entering calculate summary statistics (" + x.length + " Number values);");

    double[] nx = new double[8];
    //("mean", "medn", "mode", "vald", "invd", "min", "max", "stdev");

    Float testNanValue = new Float(Float.NaN);
    Number testNumberValue = testNanValue;
    if (Double.isNaN(testNumberValue.doubleValue())) {
        logger.fine("Float test NaN value is still recognized as a Double NaN.");
    }//from  w  w  w.j  av a2 s . co m

    int invalid = countInvalidValues(x);
    nx[4] = invalid;
    logger.fine("counted invalid values: " + nx[4]);
    nx[3] = x.length - invalid;
    logger.fine("counted valid values: " + nx[3]);

    //double[] newx = prepareForSummaryStats(x);
    double[] newx = prepareForSummaryStatsAlternative(x, x.length - invalid);
    logger.fine("prepared double vector for summary stats calculation (" + newx.length + " double values);");

    ////nx[0] = StatUtils.mean(newx);
    nx[0] = calculateMean(newx);
    logger.fine("calculated mean: " + nx[0]);
    ////nx[1] = StatUtils.percentile(newx, 50);
    nx[1] = calculateMedian(newx);
    logger.fine("calculated medn: " + nx[1]);
    nx[2] = 0.0; //getMode(newx); 

    nx[5] = StatUtils.min(newx);
    logger.fine("calculated min: " + nx[5]);
    nx[6] = StatUtils.max(newx);
    logger.fine("calculated max: " + nx[6]);
    nx[7] = Math.sqrt(StatUtils.variance(newx));
    logger.fine("calculated stdev: " + nx[7]);
    return nx;
}

From source file:es.bsc.amon.util.tree.TreeNodeFactory.java

public static JsonNode toJson(TreeNode n) {
    JsonNode json = null;// w  w w. j a v  a 2  s  .c o m
    if (n instanceof ObjNode) {
        json = new ObjectNode(JsonNodeFactory.instance);
        for (Map.Entry<String, TreeNode> e : ((ObjNode) n).properties.entrySet()) {
            ((ObjectNode) json).put(e.getKey(), toJson(e.getValue()));
        }
    } else if (n instanceof NodeArray) {
        json = new ArrayNode(JsonNodeFactory.instance);
        for (TreeNode ne : ((NodeArray) n).elements) {
            ((ArrayNode) json).add(toJson(ne));
        }
    } else if (n instanceof StringValue) {
        json = new TextNode(((StringValue) n).value);
    } else if (n instanceof NumberValue) {
        Number val = ((NumberValue) n).value;
        if (val instanceof Byte || val instanceof Short || val instanceof Integer || val instanceof Long) {
            json = new LongNode(val.longValue());
        } else {
            json = new DoubleNode(val.doubleValue());
        }
    } else
        throw new RuntimeException("You should not reach this");
    return json;
}

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//w  w w.j  av a 2s .com
 * @return
 */
public static int intValueExact(Number n) {
    if (n instanceof Integer || n instanceof Short || n instanceof Byte)
        return n.intValue();
    double d = n.doubleValue();
    long l = n.longValue();
    if (d == (double) l) {
        if (l >= Integer.MIN_VALUE && l <= Integer.MAX_VALUE)
            return (int) l;
    }
    throw new NumberFormatException();
}

From source file:org.jfree.data.DataUtilities.java

/**
 * Returns the total of the values in one row of the supplied data
 * table.//w ww. java  2s  .  c  om
 *
 * @param data  the table of values (<code>null</code> not permitted).
 * @param row  the row index (zero-based).
 *
 * @return The total of the values in the specified row.
 */
public static double calculateRowTotal(Values2D data, int row) {
    ParamChecks.nullNotPermitted(data, "data");
    double total = 0.0;
    int columnCount = data.getColumnCount();
    for (int c = 0; c < columnCount; c++) {
        Number n = data.getValue(row, c);
        if (n != null) {
            total += n.doubleValue();
        }
    }
    return total;
}

From source file:org.jfree.data.DataUtils.java

/**
 * Returns the total of the values in one row of the supplied data
 * table./*from   w w w . jav  a2 s .c  o  m*/
 *
 * @param data  the table of values ({@code null} not permitted).
 * @param row  the row index (zero-based).
 *
 * @return The total of the values in the specified row.
 */
public static double calculateRowTotal(Values2D data, int row) {
    Args.nullNotPermitted(data, "data");
    double total = 0.0;
    int columnCount = data.getColumnCount();
    for (int c = 0; c < columnCount; c++) {
        Number n = data.getValue(row, c);
        if (n != null) {
            total += n.doubleValue();
        }
    }
    return total;
}

From source file:org.jfree.data.DataUtilities.java

/**
 * Returns the total of the values in one column of the supplied data
 * table./*www.j av  a 2 s .  com*/
 *
 * @param data  the table of values (<code>null</code> not permitted).
 * @param column  the column index (zero-based).
 *
 * @return The total of the values in the specified column.
 */
public static double calculateColumnTotal(Values2D data, int column) {
    ParamChecks.nullNotPermitted(data, "data");
    double total = 0.0;
    int rowCount = data.getRowCount();
    for (int r = 0; r < rowCount; r++) {
        Number n = data.getValue(r, column);
        if (n != null) {
            total += n.doubleValue();
        }
    }
    return total;
}

From source file:org.jfree.data.DataUtils.java

/**
 * Returns the total of the values in one column of the supplied data
 * table./*from  w  w w. j av  a  2 s .  c  o m*/
 *
 * @param data  the table of values ({@code null} not permitted).
 * @param column  the column index (zero-based).
 *
 * @return The total of the values in the specified column.
 */
public static double calculateColumnTotal(Values2D data, int column) {
    Args.nullNotPermitted(data, "data");
    double total = 0.0;
    int rowCount = data.getRowCount();
    for (int r = 0; r < rowCount; r++) {
        Number n = data.getValue(r, column);
        if (n != null) {
            total += n.doubleValue();
        }
    }
    return total;
}

From source file:driimerfinance.helpers.FinanceHelper.java

/**
 * unformats a formatted number to be a double again
 * /*from   w ww. j av a 2  s  .co  m*/
 * @param string number to convert
 * @return an unformatted number to be used for db interactions
 */
public static double unformatAmount(String formattedNumber) {
    NumberFormat numberFormatter = NumberFormat.getCurrencyInstance();
    Number number = null;
    try {
        number = numberFormatter.parse(formattedNumber);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    double unformattedNumber = number.doubleValue();
    return unformattedNumber;
}