Convert string value to float value
static float parseFloat(String s)
- Returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.
static Float valueOf(float f)
- Returns a Float instance representing the specified float value.
static Float valueOf(String s)
- Returns a Float object holding the float value represented by the argument string s.
public class Main {
public static void main(String[] args) {
Float floatObject2 = Float.parseFloat("1.1F");
System.out.println(floatObject2);
}
}
The output:
1.1
You cannot use floating-point literal as Java think it is a double.
public class Main {
public static void main(String[] args) {
Float floatObject2 = Float.valueOf(1.1);
System.out.println(floatObject2);
}
}
When compiling it, the compile generates error message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method valueOf(String) in the type Float is not applicable for the arguments (double)
at Main.main(Main.java:3)
Home
Java Book
Essential Classes
Java Book
Essential Classes
Float:
- Float class
- MAX/MIN_VALUE Find out the Maximum value and Minimum value a float type can have
- Create a Float object
- Convert Float to byte, double, float, int, long and short
- Compare two float objects
- Infinite and Not A Number
- Convert float value to Hex String value
- Convert float value to String value
- Convert string value to float value
- Bit oriented calculation for float