Double.valueOf() String in Java

Double.valueOf(): This method is available in java.lang.Double class of Java.

Syntax:

Double java.lang.Double.valueOf(String s) throws NumberFormatException

This method takes one argument of type String as its parameter. This method returns a Double object holding the double value represented by the argument string s.

Note: If s is null, then a NullPointerException is thrown.

Parameters: One parameter is required for this method.

s: the string to be parsed.

Returns: a Double-object holding the value represented by the String argument.

Throws: NumberFormatException - if the string does not contain a parsable number.

For Example:

String s = "1818.56"

Double.valueOf(s) = > It returns 1818.56

Approach

Java

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

        String s = "1818.56";
        System.out.println(Double.valueOf(s));
    }
}

Output:

1818.56

No comments:

Post a Comment