How to Convert a String value to Float value in Java with Examples
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); } } |
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); } } |
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); } } |
1.0 after converting into float = 1.0
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- How to Convert a Float value to String value in Java with Examples
- Java Guava | Floats.indexOf(float[] array, float[] target) method with Examples
- Java Guava | Floats.indexOf(float[] array, float target) method with Examples
- How to Convert a String value to Short value in Java with Examples
- How to Convert a String value to Byte value in Java with Examples
- How to Convert a Byte value to String value in Java with Examples
- How to Convert a Double value to String value in Java with Examples
- How to Convert a Short value to String value in Java with Examples
- Float floatValue() in Java with examples
- Float longValue() in Java with Examples
- Float compareTo() method in Java with examples
- Float doubleValue() method in Java with examples
- Float byteValue() method in Java with Examples
- Float compare() Method in Java with Examples
- Float isNaN() method in Java with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

