String类型的double值,转换为Integer类型

String str = "123.0001";
Double d =   Double.parseDouble(str);
Integer val1 = d.intValue();
System.out.println("方法一:转double再转int:  " +val1);
val1 =   Integer.valueOf(str.replaceAll("\\.\\d*$","")).intValue();
System.out.println("方法二:用正则替换小数点和后面的数字  :" +val1);
val1 =   Integer.parseInt(str.replaceAll("\\.\\d*$",""));
System.out.println("方法二的另外一个方法:  " +val1);

 

输出:

  方法一:转double再转int: 123
  方法二:用正则替换小数点和后面的数字 :123
  方法二的另外一个方法: 123

posted @ 2020-03-24 16:27  蜜铀  阅读(1082)  评论(0编辑  收藏  举报