Java String to float

Last Updated : 29 Sept 2026

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.

Java Convert String to float

Scenario

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.

Signature

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.

Using Float.parseFloat() Method

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.

Example

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

Using Float.valueOf() Method

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.

Example

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