Java Double parseDouble() 方法

2024 年 11 月 10 日 | 阅读 2 分钟

Java Double 类的 parseDouble 方法返回一个新创建的 double 值,该值初始化为由定义的 String 对应的取值。

此方法执行的操作与 Float 类的 valueOf() 方法相同。

语法

参数

s- 这是要解析的字符串。

返回值

parseDouble() 方法返回与传入参数对应的 double 值。

抛出

parseDouble() 方法抛出

1.NullPointerException- 如果传入的字符串为 null。

2.NumberFormatException- 如果传入的字符串不包含可解析的浮点数。

示例 1

输出

1. Sum =1000156.0
2. Sum =1156.0
3. Multiplication =156000.0
4. Division =6.410256410256411
5. Reminder =64.0

示例 2

输出

Enter your age:09
Sorry! You are not eligible.Try again after 44.0 years
Your year of birth is = 2009.0

示例 3

输出

Enter any string:Himanshu@123
Exception in thread "main" java.lang.NumberFormatException: For input string: "Himanshu@123"
	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
	at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
	at java.lang.Double.parseDouble(Double.java:538)

示例 4

输出

Enter any string:himanshu@123
An exception java.lang.NumberFormatException: For input string: "himanshu@123" has occured.
Plz enter a valid double integer