Java.lang Package Classes

Java Float - parseFloat() Method



The java.lang.Float.parseFloat() method returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.

Syntax

public static float parseFloat(String s)
                        throws NumberFormatException

Parameters

s Specify the string to be parsed.

Return Value

Returns the float value represented by the string argument.

Exception

  • Throws NullPointerException, if the string is null.
  • Throws NumberFormatException, if the string does not contain a parsable float.

Example:

In the example below, the java.lang.Float.parseFloat() method is used to parse the string argument as a float value.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating a string holding float value
    String x = "25.2";

    //creating float value
    float y = Float.parseFloat(x);

    //printing the string
    System.out.println("The string is: " + x); 

    //printing the float value
    System.out.println("The float value is: " + y);   
  }
}

The output of the above code will be:

The string is: 25.2
The float value is: 25.2

❮ Java.lang - Float