Home » Java programming language

Java Float class parseFloat() method with example

Float class parseFloat() method: Here, we are going to learn about the parseFloat() method of Float class with its syntax and example.
Submitted by Preeti Jain, on October 08, 2019

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);

Parameter(s):

  • 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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.