每个基本类型都有一个封装类 char,byte,short,int,long,float,double对应 Char,Byte,Short,Integer,Long,Float,Double 都对应一个解析String到对应基本类型的类方法:parse***char c= Char.parseChar(str) byte b = Byte.parseByte(str) short s =Short.parseShort(str)int i= Integer.parseInt(str) long l = Long.parseLong(str) Float f =Float.parseFloat(str) double d =Double.parseDouble(str)每个str都判断非null非""
double d=Double.parseDouble(s);
String str = "3.3";
double b;
try{
b=Double.parseDouble(str);
}catch(Excption e){}
String str="3.3";double b;try{
b=Double.parseDouble(str);
}catch(Exception e){}上面Exception少写了一个e了
这个, 验证的操作应该在转换之前做好,不该去抓exception了 ps:
楼主的题不值20分啊,呵呵
有 public static Double parseDouble(String s) throws NumberFomatException
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("哈哈哈");
String s = "100";
int is = Integer.parseInt(s);
printOut(is);
double ds = Double.parseDouble(s);
printOut(ds);
boolean bs = Boolean.parseBoolean(s);
printOut(bs);//不是1就是false
char[] charS = s.toCharArray();
printOut(charS);
byte[] byteS = s.getBytes();
printOut(byteS);
short shortS = Short.parseShort(s);
printOut(shortS);
long longS = Long.parseLong(s);
printOut(longS);
}
private static void printOut(Object o){
System.out.println(o);
}
private static void printOut(char[] chs){
for(int i = 0;i<chs.length;i++)
System.out.print(chs[i]);
System.out.println();
}
private static void printOut(byte[] bs){
for(int i = 0;i<bs.length;i++){
System.out.print((char)bs[i]);//byte 是?
}
System.out.println();
}
}
char,byte,short,int,long,float,double对应
Char,Byte,Short,Integer,Long,Float,Double
都对应一个解析String到对应基本类型的类方法:parse***char c= Char.parseChar(str)
byte b = Byte.parseByte(str)
short s =Short.parseShort(str)int i= Integer.parseInt(str)
long l = Long.parseLong(str)
Float f =Float.parseFloat(str)
double d =Double.parseDouble(str)每个str都判断非null非""
double d;
s="1.41";
d=Double.parseDouble(s);
double d = Double.parseDouble(str) ;
}catch(Exception e) {}
double b;
try{
b=Double.parseDouble(str.trim());
}catch(Excption e){
System.out.println("不是一个数字字符串!");
}
b=Double.parseDouble(str);
}catch(Exception e){}
up
Double parseDouble(String s)
double d=Double.parseDouble(s); // the result这个不是checked exception 可以不用try-catch block
double d;
d=Double.parseDouble(s);
或者 new Double(s).doubleValue()