在Java中将String转换为double

问题描述 投票:265回答:13

如何将String"12.34"转换为Java中的double

java string casting double type-conversion
13个回答
449
投票

你可以使用Double.parseDouble()String转换为double

String text = "12.34"; // example String
double value = Double.parseDouble(text);

对于您的情况,它看起来像你想要的:

double total = Double.parseDouble(jlbTotal.getText());
double price = Double.parseDouble(jlbPrice.getText());

3
投票

这就是我要做的

    public static double convertToDouble(String temp){
       String a = temp;
       //replace all commas if present with no comma
       String s = a.replaceAll(",","").trim(); 
      // if there are any empty spaces also take it out.          
      String f = s.replaceAll(" ", ""); 
      //now convert the string to double
      double result = Double.parseDouble(f); 
    return result; // return the result
}

例如,您输入字符串“4 53,53.0”,输出将是双号45563.0


2
投票

使用Double.parseDouble()而不包围try/catch块会导致潜在的NumberFormatException输入双字符串不符合有效格式。

Guava为此提供了一个实用方法,如果无法解析String,它将返回null

https://google.github.io/guava/releases/19.0/api/docs/com/google/common/primitives/Doubles.html#tryParse(java.lang.String)

Double valueDouble = Doubles.tryParse(aPotentiallyCorruptedDoubleString);

在运行时,格式错误的String输入会生成分配给nullvalueDouble


1
投票
String s = "12.34";
double num = Double.valueOf(s);

0
投票

当你需要int时,将它用于将任何String数转换为double,只需将数据类型从num和num2转换为int;用英语中的所有字符串加倍:“Bader Qandeel”

public static double str2doubel(String str) {
    double num = 0;
    double num2 = 0;
    int idForDot = str.indexOf('.');
    boolean isNeg = false;
    String st;
    int start = 0;
    int end = str.length();

    if (idForDot != -1) {
        st = str.substring(0, idForDot);
        for (int i = str.length() - 1; i >= idForDot + 1; i--) {
            num2 = (num2 + str.charAt(i) - '0') / 10;
        }
    } else {
        st = str;
    }

    if (st.charAt(0) == '-') {
        isNeg = true;
        start++;
    } else if (st.charAt(0) == '+') {
        start++;
    }

    for (int i = start; i < st.length(); i++) {
        if (st.charAt(i) == ',') {
            continue;
        }
        num *= 10;
        num += st.charAt(i) - '0';
    }

    num = num + num2;
    if (isNeg) {
        num = -1 * num;
    }
    return num;
}

47
投票

如果在将字符串解析为十进制值时遇到问题,则需要将数字中的“,”替换为“。”。


String number = "123,321";
double value = Double.parseDouble( number.replace(",",".") );

37
投票

要将字符串转换回double,请尝试以下操作

String s = "10.1";
Double d = Double.parseDouble(s);

parseDouble方法将实现所需的效果,Double.valueOf()方法也将如此。


36
投票
double d = Double.parseDouble(aString);

这应该将字符串aString转换为double d。


29
投票

使用new BigDecimal(string)。这将保证以后正确计算。

根据经验 - 总是使用BigDecimal进行敏感计算,如金钱。

例:

String doubleAsString = "23.23";
BigDecimal price = new BigDecimal(doubleAsString);
BigDecimal total = price.plus(anotherPrice);

20
投票

您只需要使用Double解析String值

String someValue= "52.23";
Double doubleVal = Double.parseDouble(someValue);
System.out.println(doubleVal);

14
投票

再次引用Robertiano的引用 - 因为这是迄今为止最通用和本地化的自适应版本。值得一个完整的帖子!

另外一个选项:

DecimalFormat df = new DecimalFormat(); 
DecimalFormatSymbols sfs = new DecimalFormatSymbols();
sfs.setDecimalSeparator(','); 
df.setDecimalFormatSymbols(sfs);
double d = df.parse(number).doubleValue();

8
投票
String double_string = "100.215";
Double double = Double.parseDouble(double_string);

4
投票

还有另一种方式。

Double temp = Double.valueOf(str);
number = temp.doubleValue();

Double是一个类,“temp”是一个变量。 “数字”是您要查找的最终数字。

© www.soinside.com 2019 - 2024. All rights reserved.