如何从字符串中获取浮点数?

Sab*_*eti 0 java string floating-point

是否有任何Java方法php floatval等效?

如果没有,我怎么能得到float值一的字符串在Java中?

Gho*_*man 7

float f = Float.parseFloat("25");
String s = Float.toString(25.0f);
Run Code Online (Sandbox Code Playgroud)

System.out.printf("%f", Float.parseFloat("1.0E7")); 输出 10000000.000000

请参见http://ideone.com/3o6dO

从娜塔莎回答更新

使用正则表达式

    Pattern p = Pattern.compile("([-+]?[0-9]*\\.?[0-9]+)");
    Matcher m = p.matcher("some string and then a number 123.456789 and continue");
    while (m.find()) {
  System.out.println(m.group(1));
}
Run Code Online (Sandbox Code Playgroud)