How to convert String into float in Java?
There are two ways you can convert a String into float in Java
- Using Float.parseFloat(String input) - This returns primitive float
String input = "123.45";
float output = Float.parseFloat(input);
System.out.println(output);
- Using Float.valueOf(String input) - This returns Float type
String input = "123.45";
Float output = Float.valueOf(input);
System.out.println(output);
Note: Code will through NumberFormatException if you do not pass a valid float String. Ex. if you pass "foo" that causes NumberFormatException.