Is there any Java method that equivalents to php floatval?
If not, how can I get float value of a string in Java?
Is there any Java method that equivalents to php floatval?
If not, how can I get float value of a string in Java?
float f = Float.parseFloat("25");
String s = Float.toString(25.0f);
System.out.printf("%f", Float.parseFloat("1.0E7")); outputs 10000000.000000
updated from natasha answer
use regex
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));
}
NumberFormatException: For input string: "122.34343The" and check this link too ideone.com/NWbT6FFloat#parseFloat parse String to float. But if string contain word character then it will throw NumberFormatException.
I found the solution using regex:
Pattern p = Pattern.compile("([-+]?[0-9]*\\.?[0-9]+)");
Matcher m = p.matcher("some string and then a number 123.456789 and continue");
if ( m.find() ) {
System.out.println(m.group(1)); // 123.456789
}
java.util.Scanner download.oracle.com/javase/1.5.0/docs/api/java/util/…I have given a link to learn regarding converting float value from string which are as http://www.roseindia.net/java/beginners/ConvertStringToFloat.shtml I hope this one is very helpful for you