Float.parseFloat() in Java

Float.parseFloat(): This method is available in java.lang.Float class of Java.

Syntax:

float java.lang.Float.parseFloat(String s) throws NumberFormatException

This method takes one argument of type String as its parameter. This method returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.

Parameters: One parameter is required for this method.

s: the string to be parsed.

Returns: the float value represented by the string argument.

Throws: NullPointerException - if the string is nullNumberFormatException - if the string does not contain a parsable float.

For Example:

String s = "1818.99"

Float.parseFloat(s) = > It returns 1818.99

Approach

Java

public class FloatparseFloat {
    public static void main(String[] args) {

        String s = "1818.99";
        System.out.println(Float.parseFloat(s));

    }
}

Output:

1818.99

No comments:

Post a Comment