Java - Float Class parseFloat() Method

Float class parseFloat() method: Here, we are going to learn about the parseFloat() method of Float class with its syntax and example. By Preeti Jain Last updated : March 21, 2024

Float class parseFloat() method

  • parseFloat() method is available in java.lang package.
  • parseFloat() method is used to return the float value corresponding to the given String denotation or in other words we can say this method is used to convert string value to a float value.
  • parseFloat() method is a static method, it is accessible with the class name too and if we try to access the method with the class object then also we will not get an error.
  • parseFloat() method may throw a NumberFormatException at the time of conversion from string to float.
    NumberFormatException: In this exception, when the string argument does not have a parsable float.

Syntax

public static float parseFloat (String str);

Parameters

  • String str – represents the string value to be parsed.

Return Value

The return type of this method is float, it returns the corresponding float value that represent the String value.

Example

// Java program to demonstrate the example 
// of parseFloat(String str) method of 
// Float class

public class ParseFloatOfFloatClass {
    public static void main(String[] args) {
        // Variables initialization
        String str1 = "100";
        String str2 = "6.7";

        // Object initialization
        Float f1 = new Float(str2);

        // It convert string into float by calling parseFloat() method
        // and store the result in another variable of float type
        float result = f1.parseFloat(str1);

        // Display result
        System.out.println("f1.parseFloat(str1): " + result);
    }
}

Output

f1.parseFloat(str1): 100.0

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.