Convert String to double in Java

匿名 (未验证) 提交于 2019-12-03 02:01:02

问题:

How can I convert a String such as "12.34" to a double in Java?

回答1:

You can use Double.parseDouble() to convert a String to a double:

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

For your case it looks like you want:

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


回答2:

If you have problems in parsing string to decimal values, you need to replace "," in the number to "."


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


回答3:

double d = Double.parseDouble(aString); 

This should convert the string aString into the double d.



回答4:

To convert a string back into a double, try the following

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

The parseDouble method will achieve the desired effect, and so will the Double.valueOf() method.



回答5:

Use new BigDecimal(string). This will guarantee proper calculation later.

As a rule of thumb - always use BigDecimal for sensitive calculations like money.

Example:

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


回答6:

You only need to parse String values using Double

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


回答7:

Citing the quote from Robertiano above again - because this is by far the most versatile and localization adaptive version. It deserves a full post!

Another option:

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


回答8:

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


回答9:

There is another way too.

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

Double is a class and "temp" is a variable. "number" is the final number you are looking for.



回答10:

Using Double.parseDouble() without surrounding try/catch block can cause potential NumberFormatException had the input double string not conforming to a valid format.

Guava offers a utility method for this which returns null in case your String can't be parsed.

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);

In runtime, a malformed String input yields null assigned to valueDouble



回答11:

This is what I would do

    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 } 

For example you input the String "4 55,63. 0 " the output will the double number 45563.0



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!