我如何能转换一个字符串,如“12.34”到双在Java?
当前回答
double d = Double.parseDouble(aString);
这将把字符串aString转换为双d。
其他回答
这就是我要做的
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 55,63”。0 " the 输出将双数字45563.0
还有另一种方法。
Double temp = Double.valueOf(str);
number = temp.doubleValue();
Double是一个类,“temp”是一个变量。 “number”是你要找的最终数字。
你可以使用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());
您只需要使用Double解析String值
String someValue= "52.23";
Double doubleVal = Double.parseDouble(someValue);
System.out.println(doubleVal);
double d = Double.parseDouble(aString);
这将把字符串aString转换为双d。
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- Printf与std::字符串?
- 在Jar文件中运行类
- 带参数的可运行?
- 不区分大小写的“in”
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- 如何在PHP中截断字符串最接近于一定数量的字符?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作