Example usage for java.lang Double parseDouble

List of usage examples for java.lang Double parseDouble

Introduction

In this page you can find the example usage for java.lang Double parseDouble.

Prototype

public static double parseDouble(String s) throws NumberFormatException 

Source Link

Document

Returns a new double initialized to the value represented by the specified String , as performed by the valueOf method of class Double .

Usage

From source file:Main.java

/**
 * Parses an xs:duration attribute value, returning the parsed duration in milliseconds.
 *
 * @param value The attribute value to parse.
 * @return The parsed duration in milliseconds.
 *//*from  w w  w  .  ja v  a  2 s  .co m*/
public static long parseXsDuration(String value) {
    Matcher matcher = XS_DURATION_PATTERN.matcher(value);
    if (matcher.matches()) {
        boolean negated = !TextUtils.isEmpty(matcher.group(1));
        // Durations containing years and months aren't completely defined. We assume there are
        // 30.4368 days in a month, and 365.242 days in a year.
        String years = matcher.group(3);
        double durationSeconds = (years != null) ? Double.parseDouble(years) * 31556908 : 0;
        String months = matcher.group(5);
        durationSeconds += (months != null) ? Double.parseDouble(months) * 2629739 : 0;
        String days = matcher.group(7);
        durationSeconds += (days != null) ? Double.parseDouble(days) * 86400 : 0;
        String hours = matcher.group(10);
        durationSeconds += (hours != null) ? Double.parseDouble(hours) * 3600 : 0;
        String minutes = matcher.group(12);
        durationSeconds += (minutes != null) ? Double.parseDouble(minutes) * 60 : 0;
        String seconds = matcher.group(14);
        durationSeconds += (seconds != null) ? Double.parseDouble(seconds) : 0;
        long durationMillis = (long) (durationSeconds * 1000);
        return negated ? -durationMillis : durationMillis;
    } else {
        return (long) (Double.parseDouble(value) * 3600 * 1000);
    }
}

From source file:blue.soundObject.jmask.Random.java

public static Generator loadFromXML(Element data) {
    Random retVal = new Random();

    Elements nodes = data.getElements();

    while (nodes.hasMoreElements()) {
        Element node = nodes.next();
        String nodeName = node.getName();

        if (nodeName.equals("min")) {
            retVal.min = Double.parseDouble(node.getTextString());
        } else if (nodeName.equals("max")) {
            retVal.max = Double.parseDouble(node.getTextString());
        }//  w  w w.  j  ava 2 s. c o m
    }

    return retVal;
}

From source file:org.jfree.chart.demo.DataParser.java

public boolean parseString(String data) {
    String[] input = data.split(" ");
    coords[0] = Integer.parseInt(input[0]);
    coords[1] = Integer.parseInt(input[1]);
    //throw out bad values
    if (coords[0] == 0 || coords[0] > effectiveX)
        return false;
    if (coords[1] == 0 || coords[1] > effectiveY)
        return false;

    if (data.length() > 2) {
        time = Double.parseDouble(input[2]);
    }/* ww w .j a  v a2 s .c o  m*/
    return true;
}

From source file:Main.java

public static double getCpuUsage1() {
    try {/*from  w  ww  .  j av a  2s .  c  o m*/
        RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
        String load = reader.readLine();
        String[] toks = load.split(" ");

        double user1 = Double.parseDouble(toks[2]);
        double system1 = Double.parseDouble(toks[4]);
        double irq1 = Double.parseDouble(toks[7]);
        double idle1 = Double.parseDouble(toks[5]);
        try {
            Thread.sleep(360);
        } catch (Exception e) {
            e.printStackTrace();
        }
        reader.seek(0);
        load = reader.readLine();
        reader.close();
        toks = load.split(" ");
        double user2 = Double.parseDouble(toks[2]);
        double system2 = Double.parseDouble(toks[4]);
        double irq2 = Double.parseDouble(toks[7]);
        double idle2 = Double.parseDouble(toks[5]);

        double user_pass = user2 - user1;
        double system_pass = system2 - system1;
        double irq_pass = irq2 - irq1;
        double idle_pass = idle2 - idle1;
        double usage = (user_pass + system_pass + irq_pass) * 100.00
                / (user_pass + irq_pass + system_pass + idle_pass);
        BigDecimal b = new BigDecimal(usage);
        double res = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return res;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    return 0;

}

From source file:Data.Utilities.java

public static Double distDiff(GPSRec rec1, GPSRec rec2) {

    return haversine(Double.parseDouble(rec1.getLatitude()), Double.parseDouble(rec1.getLongitude()),
            Double.parseDouble(rec2.getLatitude()), Double.parseDouble(rec2.getLongitude()));
}

From source file:biblioteca.reportes.ChartCreator.java

public static DefaultPieDataset asignarPieDataset(ArrayList<String> Valores) {
    DefaultPieDataset dataSet = new DefaultPieDataset();
    int size = Valores.size();
    size = (size <= 32) ? size : 32;//from  www  . j  a  va  2s .  c  om
    for (int i = 2; i < size; i += 2) {
        dataSet.setValue(Valores.get(i), Double.parseDouble(Valores.get(i + 1)));
    }
    return dataSet;
}

From source file:net.servicestack.client.Utils.java

public static Double tryParseDouble(String str) {
    try {// www. j  a v  a2s  .  c om
        return Double.parseDouble(str);
    } catch (Exception ex) {
        return null;
    }
}

From source file:Main.java

/** Locate a sub-element tagged 'name', return its value.
 *
 *  @param element Element where to start looking. May be null.
 *  @param name Name of sub-element to locate.
 *  @param default_value Default value if not found
 *
 *  @return Returns double string that was found
 *  @throws Exception on error in number format
 *//*from   w  w w  .j  a  v a2 s  . c  o  m*/
final public static double getSubelementDouble(final Element element, final String name,
        final double default_value) throws Exception {
    final String text = getSubelementString(element, name, "").trim();
    if (text.isEmpty())
        return default_value;
    try {
        return Double.parseDouble(text);
    } catch (NumberFormatException ex) {
        throw new Exception("Invalid number for <" + name + ">", ex);
    }
}

From source file:api.util.JsonUtil.java

public static Number asNumber(Object val) {
    if (val instanceof Number)
        return (Number) val;
    if (val == JSONObject.NULL)
        return null;
    if (val instanceof String) {
        String str = (String) val;
        try {// w  w w . jav a 2s .c  o m
            return Integer.parseInt(str);
        } catch (Exception ex) {
        }
        try {
            return Long.parseLong(str);
        } catch (Exception ex) {
        }
        try {
            return Double.parseDouble(str);
        } catch (Exception ex) {
        }
    }
    throw new IllegalArgumentException("Not a number: " + val);
}

From source file:edu.cmu.tetrad.cli.util.Args.java

public static double getDouble(String value) {
    try {//  w w w  .  j  ava 2s.  c o  m
        return Double.parseDouble(value);
    } catch (NumberFormatException exception) {
        throw new IllegalArgumentException(String.format("'%s' is not a double.", value));
    }
}