How to Convert String to float in Java
How to Convert String to Float in java
It is used if you want to perform mathematical operations on the string that contains float number. You can convert String to float in the following ways:
- Convert using Float.parseFloat() method
- Convert using Float.valueOf() method
- Convert using constructor new Float(String).floatValue()
Convert using Float.parseFloat()
The parseFloat() is the static method of Float class parses the String argument and returns a float value. The value that method returns is not an instance of Float class; it just a primitive float value. This is the most common and popular method to convert String to float. The signature of the parseFloat() method is given below:
public static int parseFloat(String s)
The parameter s will be converted to a primitive float value.
Example
In the following example, str is a variable of type String having value “89.67”. f is a variable of float type that stores the converted value of str. Float is a wrapper class. The parseFloat() is a static method passing str as an argument to it and returns float value. The println statement prints the converted value of str.
public class StringToFloatExample { public static void main(String args[]) { String str="89.67"; float f=Float.parseFloat(str); System.out.println("After conversion float value is: " +f); } }
Output
After conversion float value is: 89.67
Remember: All characters must be in digits; the number can have a minus sign.
Valid Conversion
- String number=”-23.67”;
- String number=”23.67”;
Invalid Conversion
- String number=”369.90a”;
- String number=” a 369.90”;
If you defines a String as above, Float.parseFloat() throws “NumberFormatException,” because it is not a valid conversion.
Convert using Float.valueOf() method
The Float.valueOf() is a static method that returns a Float object holding the value of the specified String. The value that method returns is an instance of Float class not a primitive type. The signature of the method is given below-
public static Float valueOf(String s)
Example
Consider the example, avg is a variable of type String having value “225.92”. num is a variable of float type that stores the converted value of str. Float is a wrapper class. The Float.valueOf() is a static method passing avgas an argument to it and returns a float value. The println statement prints the converted value of num.
public class StringToFloatExample1 { public static void main(String args[]) { String avg = "225.92"; float num= Float.valueOf(avg); System.out.println("converted value of avg is: " + num); } }
Output
After conversion value of avg is: 225.92
Convert using constructor new Float(String).floatValue()
It is an alternative method is to create an instance of Float class and then invoke it's floatValue() method. You can create constructor in the following ways:
String str= "67.34"; Float floatObject = new Float(str); float value= floatObject.floatValue();
You can also write above code as:
String str = "67.34"; float value = new Float(str).floatValue();
Or
float value = new Float("67.43").floatValue();
Example
Consider the example, val is a variable of type String having value “876.234”. num is a variable of float type that stores the converted value of val. The new is a keyword that creates the constructor of the Float class. It has a val as an argument passing to it. It invokes the float.value() method of the Float class. The println statement prints the converted value of num.
public class StringToFloatExample2 { public static void main(String args[]) { String val= "876.234"; float num = new Float(val).floatValue(); System.out.println("Converted value is: " + num); } }
Output
Converted value is: 876.234