在Java中将字符串转换为双精度

在Java中将字符串转换为双精度,java,string,casting,double,type-conversion,Java,String,Casting,Double,Type Conversion,如何在Java中将字符串(如12.34)转换为双精度?您可以使用将字符串转换为双精度: 对于您的情况,看起来您需要: double total = Double.parseDouble(jlbTotal.getText()); double price = Double.parseDouble(jlbPrice.getText()); 这应该将字符串aString转换为双d。使用新的BigDecimalstring。这将保证以后正确计算 作为一条经验法则-对于诸如金钱之类的敏感计算,始终使用B

如何在Java中将字符串(如12.34)转换为双精度?

您可以使用将字符串转换为双精度:

对于您的情况,看起来您需要:

double total = Double.parseDouble(jlbTotal.getText());
double price = Double.parseDouble(jlbPrice.getText());
这应该将字符串aString转换为双d。

使用新的BigDecimalstring。这将保证以后正确计算

作为一条经验法则-对于诸如金钱之类的敏感计算,始终使用BigDecimal

例如:

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

还有另一种方法

Double temp = Double.valueOf(str);
number = temp.doubleValue();
Double是一个类,temp是一个变量。 number是您要查找的最后一个数字。

您只需要使用Double解析字符串值


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


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

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

parseDouble方法将达到预期效果,Double.valueOf方法也将达到预期效果。

再次引用上面Robertiano的引文,因为这是迄今为止最通用和本地化自适应的版本。它应该得到一个完整的职位

另一种选择:

DecimalFormat df = new DecimalFormat(); 
DecimalFormatSymbols sfs = new DecimalFormatSymbols();
sfs.setDecimalSeparator(','); 
df.setDecimalFormatSymbols(sfs);
double d = df.parse(number).doubleValue();
使用Double.parseDouble而不使用try/catch块可能会导致输入双精度字符串不符合有效格式的潜在NumberFormatException

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

Double valueDouble=Doubles.tryParseaPotentiallyCorruptedDoubleString;


在运行时,一个格式错误的字符串输入会将null赋值给valueDouble,这就是我要做的

    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
}
例如,输入字符串455,63。0
输出将是双精度数字45563.0

用于在需要int时将任何字符串数字转换为双精度,只需将数据类型从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;
}
试试这个, BigDecimalBDVal=新的BigDecimalstr

如果你只想要双倍,那就试试吧 Double d=Double.valueOfstr;
System.out.printlnString.format%.3f,新的大小数

所以我的编码应该是double total=double.parseDoublestring@TinyBelly:是的,看起来你想要:double total=double.parseDoublejlbTotal.getText@TinyBelly:您需要从字符串中提取要为double解析的文本部分。这里有一种方法可以用于您的案例,但我不能保证它适用于所有情况:double total=double.parseDoublejlbTotal.getText.replaceAll[^0-9.],;-这基本上会替换所有不是数字或数字的字符。若为nothing,则只保留要分析的数字和小数点。不建议使用double进行价格计算。对于在搜索类double而不是基元类型double的结果时在此处引诱的用户,请使用double.valueOfString.aString是指什么?你能帮我解释一下吗?阿斯汀只是一根绳子。更具体地说,是要转换为数字的字符串;DecimalFormatSymbols sfs=新的DecimalFormatSymbols;sfs.setDecimalSeparator',';df.setDecimalFormatSymbolsFs;df.parsenumber;否,String.valueOfsomething返回某物的字符串表示形式:如果某物是内置类型,或者如果它是对象且为null,则它等效于someObject.toString。要从字符串中获取Double,必须按照其他答案中所示的相反方式进行。Double.valueOfsomeString返回Double,而Double.parseDoublesomeString返回Double。基本上,只是复制几年前发布的投票率最高的答案,肯定会有大量的上升票。由于自动装箱/取消装箱,从Java5开始,您可以将一个Double分配给一个Double。相关:如果您的字符串无法解析,则返回0.0-Javadoc不支持该声明。@Tom它返回空值双精度类型,可以转换为0.0双精度类型–DYS 13秒前编辑它可以,但不会自动执行,并且您的代码段无法执行此操作。
String s = "10.1";
Double d = Double.parseDouble(s);
DecimalFormat df = new DecimalFormat(); 
DecimalFormatSymbols sfs = new DecimalFormatSymbols();
sfs.setDecimalSeparator(','); 
df.setDecimalFormatSymbols(sfs);
double d = df.parse(number).doubleValue();
String double_string = "100.215";
Double double = Double.parseDouble(double_string);
    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
}
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;
}
String s = "12.34";
double num = Double.valueOf(s);