Java Data Type How to - Get Double number from string








Question

We would like to know how to get Double number from string.

Answer

import java.util.regex.Matcher;
import java.util.regex.Pattern;
//from  w w w  .  j a  va  2  s.  c om
public class Main {
  public static void main(String[] args) {
    String str = "This is a 23.4.123 test.";
    String[] s = str.split(" ");
    Pattern p = Pattern.compile("(\\d)+\\.(\\d)+");
    double d;
    for (int i = 0; i < s.length; i++) {
      Matcher m = p.matcher(s[i]);
      while (m.find()) {
        d = Double.parseDouble(m.group());
        System.out.println(d);
      }
    }
  }
}

The code above generates the following result.