String to float conversion is generally used if we have to perform some mathematical calculations on string data that contains float type numbers. There are situations in which we read data in the form of a string, and we need to convert it into float to perform required operations. In these situations, we need to use Java API’s to convert string types to float.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
There are mainly four different ways to convert string to float in java:
1. Float.valueOf ()
public static Float valueOf(String input) throws NumberFormatException
In the above syntax, input is the string that is to be converted to float. The return type of the above method is an object of the Float wrapper class.
2. Float.parseFloat()
public static float parseFloat(String input) throws NumberFormatException
In the above syntax, input is the string that is to be converted to float. The return type of the above method is a float value.
3. Using constructor
new Float(String input)
Calling the above constructor with string to be converted to float as input gives float value corresponding to the given string.
4. text.DecimalFormat
DecimalFormat format= new DecimalFormat (“#”); format.parse(String input).floatValue();
In the above method, first an instance of java.text.DecimalFormat is created, and then the parse method is involved accepting string as input type, then followed by the floatValue () method to get float value.
Note: All the above methods may throw an exception in case an invalid string is passed as input.Below are the various examples of the Java string to float:
This example shows how to use the parseFloat() method to convert string to float.
Code:
public class StringToFloatDemo{ public Float convertStringToFloat(String stringvalue){ try{ float floatValue= Float.parseFloat(stringvalue); return floatValue; } catch(NumberFormatException e){ System.out.println ("NumberFormatException occurred."); System.out.println(stringvalue + " is not a valid number."); return null; } } public static void main(String args[]) { StringToFloatDemo demo = new StringToFloatDemo(); String input= "201.5"; float output= demo.convertStringToFloat("201.50"); System.out.println("Float Value of " + input + " is : " + output); } }
Output:
In case of an exception, let us consider if “Edubca” is passed as input to convertStringToFloat function, then it will show the following output:
In this example, we will show how to use the constructor approach to convert string to float.
Code:
public class StringToFloatDemo{ public Float convertStringToFloat(String stringvalue){ try{ float floatValue = new Float(stringvalue); return floatValue; } catch(NumberFormatException e){ System.out.println ("NumberFormatException occurred."); System.out.println(stringvalue + " is not a valid number."); return null; } } public static void main(String args[]) { StringToFloatDemo demo = new StringToFloatDemo(); String input= "200"; float output= demo.convertStringToFloat(input); System.out.println("Float Value of " + input + " is : " + output); } }
Output:
Similar to example 1, if an invalid string is passed as input, then NumberFormatException will be generated.
In this example, we will show how to use the valueOf() method of the Float class to convert string to float.
Code:
public class StringToFloatDemo{ public Float convertStringToFloat(String stringvalue){ float floatValue; try{ floatValue= Float.valueOf(stringvalue); return floatValue; }catch (NumberFormatException e) { System.out.println ("NumberFormatException occurred."); System.out.println(stringvalue + " is not a valid number."); return null; } } public static void main(String args[]) { StringToFloatDemo demo = new StringToFloatDemo(); String input= "200"; float output= demo.convertStringToFloat(input); System.out.println("Float Value of " + input + " is : " + output); } }
Output:
Similar to example 1 and 2, if an invalid string is passed as input, then NumberFormatException will be generated.
In this example, we will show how to use the DecimalFormat class to convert string to float.
Code:
import java.text.DecimalFormat; import java.text.ParseException; public class StringToFloatDemo{ public Float convertStringToFloat(String stringvalue){ float floatValue; DecimalFormat decimalFormat = new DecimalFormat("#"); try { floatValue = decimalFormat.parse(stringvalue).floatValue(); return floatValue; } catch (ParseException e) { System.out.println("Parse Exception Occurred"); System.out.println(stringvalue + " is not a valid number."); return null; } } public static void main(String args[]) { StringToFloatDemo demo = new StringToFloatDemo(); String input= "200"; float output= demo.convertStringToFloat(input); System.out.println("Float Value of " + input + " is : " + output); } }
Output:
In case an invalid String is passed as input, then Parse Exception will be generated.
Let us consider the case when we pass “Edubca” instead of 200 as input to the above program; then the below error will be generated:
The above is the detailed content of Java String to Float. For more information, please follow other related articles on the PHP Chinese website!