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

public static double DecimalToDouble(String dec) {
    //replace all commas with periods
    String commaReplaced = dec.replace(',', '.');
    return Double.parseDouble(commaReplaced);
}

From source file:Main.java

public static double[] readVectorDouble(String path) throws FileNotFoundException {
    ArrayList<Double> arr = new ArrayList<Double>();
    System.err.println("Load vector from " + path);
    Scanner sc = new Scanner(new BufferedReader(new FileReader(path)));
    while (sc.hasNext()) {
        String line = sc.nextLine();
        if (line.isEmpty()) {
            break;
        }/*from ww w.  j  a v a2  s  . c  o  m*/
        if (line.startsWith("%%")) { // detect matrix market format
            System.err.println(line);
            while (sc.nextLine().startsWith("%"))
                ; // skip the comment line, and the dimension info.
            continue;
        }
        arr.add(Double.parseDouble(line));
    }
    System.err.println("Length: " + arr.size());
    double[] ret = new double[arr.size()];
    for (int i = 0; i < arr.size(); i++)
        ret[i] = arr.get(i);
    System.err.println("Done.");
    return ret;
}

From source file:IO.java

public static double[][] readDoubleMat(File file, int row, int col) {
    double[][] data = new double[row][col];
    try {//w w w . j a  va2  s .c  o m
        Scanner sc = new Scanner(file);
        sc.useDelimiter(",|\\n");

        for (int i = 0; i < row; i++)
            for (int j = 0; j < col; j++) {
                data[i][j] = Double.parseDouble(sc.next());
            }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return data;
}

From source file:Main.java

/**
 * Gets the double value of a named attribute of a node.
 * @param node Node The context node.//from  w  w w.j a  v a 2  s  .c  o  m
 * @param name String
 * @param errValue double
 * @return double
 */
public static double getAttributeDouble(Node node, String name, double errValue) {
    String s = getAttributeString(node, name);
    if (s != null && s.length() > 0)
        return Double.parseDouble(s);
    else
        return errValue;
}

From source file:Main.java

private static Object convertValType(Object value, Class fieldTypeClass) {
    Object retVal = null;/*ww w  .  ja va2s  . c  o  m*/
    if (Long.class.getName().equals(fieldTypeClass.getName())
            || long.class.getName().equals(fieldTypeClass.getName())) {
        retVal = Long.parseLong(value.toString());
    } else if (Integer.class.getName().equals(fieldTypeClass.getName())
            || int.class.getName().equals(fieldTypeClass.getName())) {
        retVal = Integer.parseInt(value.toString());
    } else if (Float.class.getName().equals(fieldTypeClass.getName())
            || float.class.getName().equals(fieldTypeClass.getName())) {
        retVal = Float.parseFloat(value.toString());
    } else if (Double.class.getName().equals(fieldTypeClass.getName())
            || double.class.getName().equals(fieldTypeClass.getName())) {
        retVal = Double.parseDouble(value.toString());
    } else {
        retVal = value;
    }
    return retVal;
}

From source file:Main.java

public static float getTotalRAM() {
    RandomAccessFile reader = null;
    String load = null;/*  ww  w.j av  a 2s.com*/
    DecimalFormat twoDecimalForm = new DecimalFormat("#.##");
    double totRam = 0;
    float lastValue = 0;
    try {
        reader = new RandomAccessFile("/proc/meminfo", "r");
        load = reader.readLine();

        // Get the Number value from the string
        Pattern p = Pattern.compile("(\\d+)");
        Matcher m = p.matcher(load);
        String value = "";
        while (m.find()) {
            value = m.group(1);
            // System.out.println("Ram : " + value);
        }
        reader.close();

        totRam = Double.parseDouble(value);
        // totRam = totRam / 1024;

        float mb = (float) (totRam / 1024.0f);
        float gb = (float) (totRam / 1048576.0f);
        float tb = (float) (totRam / 1073741824.0f);

        lastValue = gb;
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        // Streams.close(reader);
    }

    return lastValue;
}

From source file:Main.java

/**
 * This method parses the given value into the specified primitive or wrapper class.
 * @param clazz - primitive or wrapper class used to parse
 * @param value - string to be parsed//from w  w  w  .  j  a  v  a2  s.  co  m
 * @return object of type clazz parsed from the string
 * @author Trisan Bepler
 */
public static Object toObject(Class<?> clazz, String value) {
    if (Boolean.TYPE == clazz)
        return Boolean.parseBoolean(value);
    if (Byte.TYPE == clazz)
        return Byte.parseByte(value);
    if (Short.TYPE == clazz)
        return Short.parseShort(value);
    if (Integer.TYPE == clazz)
        return Integer.parseInt(value);
    if (Long.TYPE == clazz)
        return Long.parseLong(value);
    if (Float.TYPE == clazz)
        return Float.parseFloat(value);
    if (Double.TYPE == clazz)
        return Double.parseDouble(value);
    if (Boolean.class == clazz)
        return Boolean.parseBoolean(value);
    if (Byte.class == clazz)
        return Byte.parseByte(value);
    if (Short.class == clazz)
        return Short.parseShort(value);
    if (Integer.class == clazz)
        return Integer.parseInt(value);
    if (Long.class == clazz)
        return Long.parseLong(value);
    if (Float.class == clazz)
        return Float.parseFloat(value);
    if (Double.class == clazz)
        return Double.parseDouble(value);
    if (Character.class == clazz)
        return value.charAt(0);
    if (Character.TYPE == clazz)
        return value.charAt(0);
    return value;
}

From source file:Main.java

public static double readDoubleAttr(Element element, String attributeName, double defaultValue) {
    String attributeValue = element.getAttribute(attributeName);
    try {/*from w w w .  ja va 2  s .c o  m*/
        return Double.parseDouble(attributeValue);
    } catch (NumberFormatException e) {
        return defaultValue;
    }
}

From source file:Main.java

/**
 * Calls getTextValue and returns a double value
 *//*  ww  w. j a  v  a  2s . c o m*/
public static double getDoubleValue(Element ele, String tagName) {
    return Double.parseDouble(getTextValue(ele, tagName));
}

From source file:Main.java

/**
 * Get a double from the value received/*from ww w .j  ava 2  s .  c o m*/
 * @param value Object to convert
 * @return int converted
 */
public static double getDouble(Object value) {
    if (value instanceof Integer) {
        return ((Integer) value).doubleValue();
    } else if (value instanceof Double) {
        return ((Double) value).doubleValue();
    } else if (value instanceof Float) {
        return ((Float) value).doubleValue();
    } else {
        try {
            return Double.parseDouble(value.toString());
        } catch (NumberFormatException nfe) {
            return 0;
        }
    }
}