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

/**
 * Tell whether the string contains a number.
 *//*from  ww  w .ja  v  a  2  s. c o  m*/
public static boolean isNumber(String string) {
    String s = string.trim();
    if (s.length() < 1)
        return false;
    double value = 0;
    try {
        value = Double.parseDouble(s);
    } catch (NumberFormatException e) {
        return false;
    }
    return true;
}

From source file:Main.java

/**
 * /* w  w w .  j a  v  a2s.  c  om*/
 * @Title: convertValType
 * @Description: TODO
 * @param value
 * @param fieldTypeClass
 * @return
 * @return: Object
 */
public static Object convertValType(Object value, Class<?> fieldTypeClass) {
    Object retVal = null;
    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:de.micromata.genome.util.types.CoerceUtils.java

/**
 * To double./*ww w .  java  2s .  co  m*/
 *
 * @param o the o
 * @return the double
 */
public static double toDouble(Object o) {
    if (o instanceof Number) {
        return ((Number) o).doubleValue();
    }
    if (o instanceof String) {
        return Double.parseDouble((String) o);
    }
    throw new RuntimeException("Cannot coerce value to double: " + Objects.toString(o, StringUtils.EMPTY));
}

From source file:Main.java

/** Helper method - gets a named attribute's value as a <I>double</I>.
   @param pNodeMap The <I>NamedNodeMap</I>.
   @param strName Name of the attribute.
   @return Value of the attribute./*from   w w  w  .j  a v  a 2s.  c o  m*/
 */
public static double getAttributeDouble(NamedNodeMap pNodeMap, String strName)
        throws ClassCastException, NumberFormatException {
    String strValue = getAttributeString(pNodeMap, strName);

    if (null == strValue)
        return Double.NaN;

    return Double.parseDouble(strValue);
}

From source file:br.on.daed.services.pdf.DadosMagneticos.java

public static byte[] gerarPDF(String ano, String tipo)
        throws IOException, InterruptedException, UnsupportedOperationException {

    File tmpFolder;/*from www .  j  a  v a2 s .co  m*/
    String folderName;

    Double anoDouble = Double.parseDouble(ano);
    List<String> dados = Arrays.asList(TIPO_DADOS_MAGNETICOS);

    byte[] ret = null;

    if (anoDouble >= ANO_MAGNETICO[0] && anoDouble < ANO_MAGNETICO[1] && dados.contains(tipo)) {

        do {

            folderName = "/tmp/dislin" + Double.toString(System.currentTimeMillis() * Math.random());
            tmpFolder = new File(folderName);

        } while (tmpFolder.exists());

        tmpFolder.mkdir();

        ProcessBuilder processBuilder = new ProcessBuilder("/opt/declinacao-magnetica/./gerar", ano, tipo);

        processBuilder.directory(tmpFolder);

        processBuilder.environment().put("LD_LIBRARY_PATH", "/usr/local/dislin");

        Process proc = processBuilder.start();

        proc.waitFor();

        ProcessHelper.outputProcess(proc);

        File arquivoServido = new File(folderName + "/dislin.pdf");

        FileInputStream fis = new FileInputStream(arquivoServido);

        ret = IOUtils.toByteArray(fis);

        processBuilder = new ProcessBuilder("rm", "-r", folderName);

        tmpFolder = new File("/");

        processBuilder.directory(tmpFolder);

        Process delete = processBuilder.start();

        delete.waitFor();

    } else {
        throw new UnsupportedOperationException("Entrada invlida");
    }
    return ret;
}

From source file:AIR.Common.Utilities.MathUtils.java

public static double parseAndTruncate(String doubleValue) {
    doubleValue = StringUtils.trim(doubleValue);
    return truncate(Double.parseDouble(doubleValue));
}

From source file:Main.java

public static double humanSizeToBytes(String value) {
    String scalar;//from   www  .j  a v a2s  . co  m
    int unit = 1024;
    int exp;
    char c;
    double returnValue = 0;

    String[] words = value.split("\\s+");

    //        Log.d("Debug", "words length:" + words.length);

    if (words.length == 2) {

        //            Log.d("Debug", "words[0]:" + words[0]);
        //            Log.d("Debug", "words[1]:" + words[1]);

        try {
            scalar = words[0].replace(",", ".");

            //                Log.d("Debug", "scalar:" + scalar);

            exp = "BKMGTPE".indexOf((words[1]).toCharArray()[0]);

            //                Log.d("Debug", "exp:" + exp);

            returnValue = Double.parseDouble(scalar) * Math.pow(unit, exp);

        } catch (Exception e) {
        }

    }

    return returnValue;
}

From source file:Main.java

public static double getArgAttr(final NodeList args, final String strName, final String strAttrName,
        final double dDefaultValue) {
    final String strValue = getArgAttr(args, strName, strAttrName);

    if (strValue == null) {
        return dDefaultValue;
    }/*from w  w  w. ja v  a 2 s .  com*/

    return Double.parseDouble(strValue);
}

From source file:Main.java

public static double[][] readMatrixDouble(String path) throws FileNotFoundException {
    int n = 0, p = 0;
    System.err.println("Load matrix from " + path);
    Scanner sc = new Scanner(new BufferedReader(new FileReader(path)));
    while (sc.hasNext()) {
        String line = sc.nextLine();
        if (line.startsWith("%")) {
            System.err.println(line);
        } else {/*from   w  w  w  .jav  a  2 s.c o  m*/
            String[] dim = line.split(" ");
            n = Integer.parseInt(dim[0]);
            p = Integer.parseInt(dim[1]);
            System.err.println("num rows: " + n + "\n" + "num cols: " + p);
            break;
        }
    }
    int count = 0;
    double[][] mat = new double[p][n];
    int row = 0, col = 0;
    while (sc.hasNextLine()) {
        String line = sc.nextLine();
        if (line.isEmpty()) {
            break;
        }
        Double val = Double.parseDouble(line);
        mat[col][row] = val;
        row++;
        if (row == n) {
            row = 0;
            col++;
        }
        count++;
    }
    if (count != n * p) {
        System.err.println("Parse fail: not enough elements. num rows: " + n + "\n" + "num cols: " + p
                + "\n nun elements: " + count);
        return null;
    }
    System.err.println("Done.");
    return mat;
}

From source file:Graphing.handler.java

public static JPanel getChart(String name, String xlabel, String bargraph, String ylabel, int[] x, int[] y,
        TableSet TS, boolean legend, boolean toolTips, int type, PlotOrientation p)
        throws NumberFormatException {
    if (type == 1) {
        double X[] = new double[x.length];
        double Y[] = new double[x.length];

        for (int i = 0; i < X.length; i++) {
            X[i] = Double.parseDouble(TS.getModel().getValueAt(x[i], y[0]).toString());
            Y[i] = Double.parseDouble(TS.getModel().getValueAt(x[i], y[1]).toString());
        }//from ww  w . j  a v a 2  s  .  co m

        graphXY XY = new graphXY(name, xlabel, ylabel, X, Y, legend, p, toolTips);

        return XY.getXYgraphPanel();
    } else if (type == 2) {
        String X[] = new String[x.length];
        double Y[] = new double[x.length];

        for (int i = 0; i < X.length; i++) {
            X[i] = TS.getModel().getValueAt(x[i], y[0]).toString();
            Y[i] = Double.parseDouble(TS.getModel().getValueAt(x[i], y[1]).toString());
        }

        barGraph bar = new barGraph(name, bargraph, xlabel, ylabel, X, Y, p, legend, toolTips);

        return bar.getBarGraph();
    } else {
        String X[] = new String[x.length];
        double Y[] = new double[x.length];

        for (int i = 0; i < X.length; i++) {
            X[i] = TS.getModel().getValueAt(x[i], y[0]).toString();
            Y[i] = Double.parseDouble(TS.getModel().getValueAt(x[i], y[1]).toString());
        }

        piChart PC = new piChart(name, X, Y, legend, toolTips);

        return PC.getPiePanel();
    }
}