Convert string to float?
Using Float.parseFloat()
?
class Test {
public static void main(String[] args) {
String s = "3.14";
float f = Float.parseFloat(s);
System.out.println(f);
}
}
String s = "3.14";
float f = Float.parseFloat(s);
Use Float.valueOf(String)
to do the conversion.
The difference between valueOf()
and parseFloat()
is only the return. Use the former if you want a Float
(object) and the latter if you want the float
number.