Open In App

How to Convert a String value to Float value in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

Given a String “str” in Java, the task is to convert this string to float type.

Examples:

Input: str = "1.0"
Output: 1.0

Input: str = "3.14"
Output: 3.14

Approach 1: (Naive Method)
One method is to traverse the string and add the numbers one by one to the float type. This method is not an efficient approach.

Approach 2: (Using Float.parseFloat() method)
The simplest way to do so is using parseFloat() method of Float class in java.lang package. This method takes the string to be parsed and returns the float type from it. If not convertible, this method throws error.

Syntax:

Float.parseFloat(str);

Below is the implementation of the above approach:

Example 1: To show successful conversion




// Java Program to convert string to float
  
class GFG {
  
    // Function to convert String to Float
    public static float convertStringToFloat(String str)
    {
  
        // Convert string to float
        // using parseFloat() method
        return Float.parseFloat(str);
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // The string value
        String stringValue = "1.0";
  
        // The expected float value
        float floatValue;
  
        // Convert string to float
        floatValue = convertStringToFloat(stringValue);
  
        // Print the expected float value
        System.out.println(
            stringValue
            + " after converting into float = "
            + floatValue);
    }
}


Output:

1.0 after converting into float = 1.0

Example 2: To show unsuccessful conversion




// Java Program to convert string to float
  
class GFG {
  
    // Function to convert String to Float
    public static void convertStringToFloat(String str)
    {
  
        float floatValue;
  
        try {
            // Convert string to float
            // using parseFloat() method
            floatValue = Float.parseFloat(str);
  
            // Print the expected float value
            System.out.println(
                str
                + " after converting into float = "
                + floatValue);
        }
        catch (Exception e) {
            // Print the error
            System.out.println(
                str
                + " cannot be converted to float: "
                + e.getMessage());
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // The string value
        String str1 = "";
        String str2 = null;
        String str3 = "GFG";
  
        // Convert string to float
        // using parseFloat() method
        convertStringToFloat(str1);
        convertStringToFloat(str2);
        convertStringToFloat(str3);
    }
}


Output:

cannot be converted to float: empty String
null cannot be converted to float: null
GFG cannot be converted to float: For input string: "GFG"

Approach 3: (Using Float.valueOf() method)
The valueOf() method of Float class converts data from its internal form into human-readable form.

Syntax:

Float.valueOf(str);

Below is the implementation of the above approach:

Example 1: To show successful conversion




// Java Program to convert string to float
  
class GFG {
  
    // Function to convert String to Float
    public static float convertStringToFloat(String str)
    {
  
        // Convert string to float
        // using valueOf() method
        return Float.valueOf(str);
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // The string value
        String stringValue = "1.0";
  
        // The expected float value
        float floatValue;
  
        // Convert string to float
        floatValue = convertStringToFloat(stringValue);
  
        // Print the expected float value
        System.out.println(
            stringValue
            + " after converting into float = "
            + floatValue);
    }
}


Output:

1.0 after converting into float = 1.0


Last Updated : 29 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads