We can convert a String to a float in Java using the Float.parseFloat() and Float.valueOf() methods. A String may contain a floating-point number that needs to be converted before performing mathematical operations. For example, data entered in a text field is received as a String, even when the user enters a number. Java provides these methods to convert the string value into a floating-point value.

This conversion is generally used when a String contains a floating-point number and we need to perform calculations on it. For example, if a user enters 3.14 in a text field, the value is received as a String. We need to convert it to a float before using it in mathematical operations.
The parseFloat() method is a static method of the Float class. Its signature is:
public static float parseFloat(String s)
The method returns a primitive float value. If the string does not contain a valid floating-point number, it throws a NumberFormatException.
The Float.parseFloat() method converts a String into a primitive float value. The string should contain a valid floating-point number. If the string contains an invalid value, the method throws a NumberFormatException. A try-catch block can be used to handle this exception.
In the following example, a string containing the value 3.14 is converted into a primitive float using the Float.parseFloat() method.
public class StringToFloatExample {
public static void main(String[] args) {
// Input string representing a float value
String floatString = "3.14";
try {
// Convert the string to a float using Float.valueOf() method
Float floatValue = Float.valueOf(floatString);
// Print the converted float value
System.out.println("Converted float value: " + floatValue);
} catch (NumberFormatException e) {
// Handle the case where the input string is not a valid float
System.err.println("Invalid float format: " + floatString);
e.printStackTrace();
}
}
}Output:
Converted float value: 3.14
The Float.valueOf() method converts a String into a Float object. It accepts a string containing a valid floating-point number and returns the corresponding Float object. If the string cannot be converted to a valid float value, it throws a NumberFormatException.
In the following example, a string containing the value 3.14 is converted into a Float object using the Float.valueOf() method.
public class StringToFloatExample {
public static void main(String[] args) {
// Input string representing a float value
String floatString = "3.14";
try {
// Convert the string to a float using Float.valueOf() method
Float floatValue = Float.valueOf(floatString);
// Print the converted float value using Float.valueOf() Method
System.out.println("Converted float value: " + floatValue);
} catch (NumberFormatException e) {
// Handle the case where the input string is not a valid float
System.err.println("Invalid float format: " + floatString);
e.printStackTrace();
}
}
}Output:
Converted float value: 3.14
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India