Java - Float valueOf(String) method
Description
The Java Float valueOf(String s) method returns a Float object holding the float value represented by the argument string s.If s is null, then a NullPointerException is thrown.
Declaration
Following is the declaration for java.lang.Float.valueOf() method
public static Float valueOf(String s) throws NumberFormatException
Parameters
s − This is the string to be parsed.
Return Value
This method returns a Float object holding the value represented by the String argument.
Exception
NumberFormatException − if the string does not contain a parsable number.
Getting a Float Object from a String having a Float value Carring Positive Value Example
The following example shows the usage of Float valueOf(String) method to get a Float object from a String. We've created a String with a valid float value and then using valueOf() method, we're retrieving the Float object and printing it.
package com.tutorialspoint;
public class FloatDemo {
public static void main(String[] args) {
// returns the float value represented by the string argument
String str = "50";
Float retval = Float.valueOf(str);
System.out.println("Value = " + retval);
}
}
Output
Let us compile and run the above program, this will produce the following result −
Value = 50.0
Facing Exception while Getting a Float Object from a String having a Invalid Value Example
The following example shows the usage of Float valueOf(String) method to get a Float object from a String. We've created a String with an invalid float value and then using valueOf() method, we're trying to retrieve the Float object and printing it. Program throws an exception as expected.
package com.tutorialspoint;
public class FloatDemo {
public static void main(String[] args) {
// returns the float value represented by the string argument
String str = "abc";
Float retval = Float.valueOf(str);
System.out.println("Value = " + retval);
}
}
Output
Let us compile and run the above program, this will produce the following result −
Exception in thread "main" java.lang.NumberFormatException: For input string: "abc" at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at sun.misc.FloatingDecimal.parseFloat(Unknown Source) at java.lang.Float.parseFloat(Unknown Source) at java.lang.Float.valueOf(Unknown Source) at com.tutorialspoint.FloatDemo.main(FloatDemo.java:9)
Getting a Float Object from a String having a Float value Carring Negative Value Example
The following example shows the usage of Float valueOf(String) method to get a Float object from a String. We've created a String with a valid negative float value and then using valueOf() method, we're retrieving the Float object and printing it.
package com.tutorialspoint;
public class FloatDemo {
public static void main(String[] args) {
// returns the float value represented by the string argument
String str = "-50";
Float retval = Float.valueOf(str);
System.out.println("Value = " + retval);
}
}
Output
Let us compile and run the above program, this will produce the following result −
Value = -50.0