➤ Hello World program
➤ Java Addition Program
➤ Average of 2 Numbers
➤ Average of 3 Numbers
➤ Simple Interest in Java
➤ Compound Interest
➤ Display ASCII Value
➤ Area of Circle Java
➤ Area of Rectangle
➤ Java Area of Triangle
➤ Java Swap 2 Numbers
➤ Distance b/w 2 Points
Java Flow Control Program
Java Array Programs
Java String Programs
How to convert a String to a Float In Java? In this post, we will discuss how to convert String To Float Java. We will see String to primitive float and wrapped float (Float) object conversion through the parseFloat() & valueOf() method. Also see:- How to Convert Float To String in Java
Convert String To Float Java Using parseFloat()
Like every wrapper class, the Float class also contains parseX() method to convert string type value to its primitive or wrapper object. The parseFloat() method is given as follows:-
public static float parseFloat(String s) throws NumberFormatException
:- It returns a new float initialized to the value represented by the specified String, as performed by the valueOf() method of the Float class. Internally this method calls FloatingDecimal.parseFloat(s).
How to Convert a String To a Float In Java Using parseFloat() method
public class Main {
public static void main(String[] args) {
String string = "900";
float val = Float.parseFloat(string);
System.out.println("float primitive value: " + val);
Float val1 = Float.parseFloat(string);
System.out.println("Float value: " + val1);
}
}
Output:-
float primitive value: 900.0
Float value: 900.0
Exceptions thrown by parseFloat() method
- NullPointerException:- if the string is null.
- NumberFormatException:- if the string does not contain a parsable float.
If we pass a null string then the parseFloat() method will throw NullPointerException and if the given string is not a number or floating-point value then it will throw NumberFormatException.
public class Main {
public static void StringToFloat(String string) {
try {
float floatValue = Float.parseFloat(string);
System.out.println(string + " after converting into float = "
+ floatValue);
} catch (Exception e) {
System.out.println(string + " cannot be converted to float");
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
String string1 = "";
String string2 = null;
String string3 = "0.23";
StringToFloat(string1);
System.out.println();
StringToFloat(string2);
System.out.println();
StringToFloat(string3);
System.out.println();
}
}
Output:-
cannot be converted to float
empty String
null cannot be converted to float
Cannot invoke “String.trim()” because “in” is null
0.23 after converting into float = 0.23
How to Convert String Into Float In Java Using Float.valueOf()
Float class contains valueOf() method which can be used to convert String type value to wrapped or primitive float value. The valueOf method is given as follows:-
public static Float valueOf(String s) throws NumberFormatException
:- It returns a Float object holding the float value represented by the argument string. It throws NumberFormatException if the string does not contain a parsable number.
public class Main {
public static void main(String[] args) {
String number = "0.23";
Float val1 = Float.valueOf(number);
System.out.println("Float value: " + val1);
float val2 = Float.valueOf(number);
System.out.println("float primitive value: " + val2);
}
}
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!