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

/**
 * Checks if a value is a valid number.// w  w w  .j  a  va 2  s  . com
 * @param {String} n Value to test
 * @return {Boolean} true if it is a number otherwise false
 */
public static Boolean isNumber(String n) {
    try {
        Double.parseDouble(n);
    } catch (NumberFormatException e) {
        return false;
    }
    // only got here if we didn't return false
    return true;
}

From source file:GetNumber.java

public static Number process(String s) {
    if (s.matches(".*[.dDeEfF]")) {
        try {/*from   w w  w  .  j  a  v  a  2s  .  co m*/
            double dValue = Double.parseDouble(s);
            System.out.println("It's a double: " + dValue);
            return new Double(dValue);
        } catch (NumberFormatException e) {
            System.out.println("Invalid a double: " + s);
            return NAN;
        }
    } else // did not contain . d e or f, so try as int.
        try {
            int iValue = Integer.parseInt(s);
            System.out.println("It's an int: " + iValue);
            return new Integer(iValue);
        } catch (NumberFormatException e2) {
            System.out.println("Not a number:" + s);
            return NAN;
        }
}

From source file:Main.java

public static double str2num(String s, double defVal) {
    if (s.equals("N/A"))
        return defVal;
    try {//from   w  w  w  .j a  v  a2s.  c  o  m
        s = s.trim();
        s = s.contains(" ") ? s.split(" ")[0] : s;
        return Double.parseDouble(s);
    } catch (Exception err) {
        Log.e("str2num", s, err);
        return defVal;
    }
}

From source file:Main.java

public static String getTwoHour(String st1, String st2) {
    String[] kk = null;//  w w  w .  j av  a 2s .  co m
    String[] jj = null;
    kk = st1.split(":");
    jj = st2.split(":");
    if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))
        return "0";
    else {
        double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;
        double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;
        if ((y - u) > 0)
            return y - u + "";
        else
            return "0";
    }
}

From source file:Main.java

public static double readDoubleFromStdin(String message) throws Exception {
    String tmp = readStringFromStdin(message);
    return Double.parseDouble(tmp);
}

From source file:Main.java

public static String formatMoneyYuan(String amount) {
    if (TextUtils.isEmpty(amount)) {
        return "0.00";
    }//w w  w  .  java2 s.c  o  m
    String result;
    try {
        double num = Double.parseDouble(amount);
        DecimalFormat formater = new DecimalFormat("###,##0.00");
        result = formater.format(num);
    } catch (NumberFormatException e) {
        result = amount;
    }
    return result;
}

From source file:Main.java

public static double readDouble(StringReader reader, char delimiter) throws NumberFormatException {
    String str = readString(reader, delimiter);
    if (str.length() == 0) {
        return 0.0d;
    } else {// w  ww.  j  av a2  s.co m
        return Double.parseDouble(str);
    }
}

From source file:Main.java

public static String removeZero(String s) {
    if (!TextUtils.isEmpty(s)) {
        try {/*w  ww  .  j a  va 2  s  .  co m*/

            double d = Double.parseDouble(s);
            DecimalFormat format = new DecimalFormat("0.#");
            return format.format(d);
        } catch (Exception e) {
            return "0";
        }
    } else {
        return "0";
    }

}

From source file:Main.java

public static String padString(String str) {
    if (str == null || str.isEmpty()) {
        return "0";
    } else if (str.equals(".")) {
        return "0.";
    } else {/*  w w  w .j  a  v  a  2 s  .com*/
        try {
            return String.format(Locale.ENGLISH, "%.4f", Double.parseDouble(str));
        } catch (Exception e) {
            return null;
        }
    }
}

From source file:Main.java

/**
 * Make a guess for the correct code value for the provided vat rate. Guessing is necessary as
 * the correct code is not part of the XML invoice.
 * //from  ww  w.j  ava2s.c  o m
 * @param vatRate
 * @return
 */
public static int guessVatCode(String vatRate) {
    if (vatRate != null && !vatRate.isEmpty()) {
        double scale = Double.parseDouble(vatRate);
        // make a guess for the correct code
        if (scale == 0)
            return 0;
        else if (scale < 7)
            return 2;
        else
            return 1;
    }
    return 0;
}