String 转int:
int x=Integer.parseInt("5");
//注意parseInt里面的参数
必须是个没有小数点的数(不能是double形式的),不然运行时会抛出异常
NumberFormatException
String 转double:
double d=Double.parseDouble("25.23542");
//注意参数一定要有小数点。。不然运行时会抛出异常 NumberFormatException
String转boolean
boolean b=new Boolean("true").booleanValue();
//这个实验的结果很奇怪,只有参数是"true"的时候会返回true,其他时候都返回false(包括"1","0"的时候都返回false)
double转String:
String str=Double.toString(2)//结果显示2.0,不像上面会报错
String str=Double.toString(2.545)
int转String类似
boolean转String:
以上parseInt等皆为
静态方法,详见:
http://blog.163.com/it_novice/blog/static/209183069201331584937375/
本文详细介绍了Java中字符串到整型、双精度型、布尔型的转换方法,以及双精度型到字符串的转换,并指出了一些常见错误和注意事项。
1244

被折叠的 条评论
为什么被折叠?



