java中String字符串转化为数字

来源:互联网 发布:伪造mac ip蹭网 编辑:程序博客网 时间:2024/06/09 16:24

java中String字符串转化为数字:

  转换为浮点型:

    使用Double或者Float的parseDouble或者parseFloat方法进行转换

1
2
3
String   s   =   "123.456 ";  //要确保字符串为一个数值,否则会出异常
double   d   =   Double.parseDouble(s); 
float   f   =   Float.parseFloat(s);

转换为整型: 

  使用Integer的parseInt方法进行转换。

1
int i = Integer.parseInt([String]);//[String]待转换的字符串
0 0
原创粉丝点击