怎么样把double类型的字符串转化为整形 如下,把a转化为int类型,除了用字符串截取的方法 String a ="545.21";int b =Integer.valueOf(a); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 long b = Math.round(Double.valueOf(a));int b = Math.round(Float.valueOf(a)); double d1=100.00; Double D1=new Double(d1); int i1=D1.intValue(); 正解!第一个是返回一个最接近参数的long第二个是返回一个最接近参数的int 已经结贴了啊你这个问的有问题,没有小数处理则怎么转整形啊。给你个写了个应对各种情况的函数。 public static enum PointCut { SISHEWURU, FLOOR, CEIL, ROUND; } public static int getIntValue(Object obj, PointCut pointMode) throws NumberFormatException { try { Double dblValue = Double.parseDouble(obj.toString()); Double forInt = 0d; int rtnIntVal; switch(pointMode) { case SISHEWURU: forInt = dblValue>=0?Math.floor(dblValue + 0.5):Math.ceil(dblValue - 0.5); break; case FLOOR: forInt = Math.floor(dblValue); break; case CEIL: forInt = Math.ceil(dblValue); break; case ROUND: forInt = dblValue; break; } Long convtedVal = Math.round(forInt); rtnIntVal = convtedVal.intValue(); return rtnIntVal; } catch (NumberFormatException ex) { System.out.print("字符串".concat(str).concat("不能转换成整数!")); throw ex; } } 有关于jsp中import指令的疑问 Ext未定义 java文件类型识别 前台用struts , 数据库用hibernate 出现了这样的错误,请帮忙。。 hibernate菜鸟提问 Oracle 恢复数据到某一时间点问题 咨询一个问题 关于2个服务器之间的数据传输 tomcat4.0运行的了卸了装5.0运行不了怎么解决 小问题!50分!! 已经用SocketServer实现了服务器端和客户端的简单通讯,想再继续修改此程序,请教各位! 利用jsp下载excel时出现乱码~!懂的来 ibeatis空指针
int b = Math.round(Float.valueOf(a));
Double D1=new Double(d1);
int i1=D1.intValue();
第一个是返回一个最接近参数的long
第二个是返回一个最接近参数的int
你这个问的有问题,没有小数处理则怎么转整形啊。
给你个写了个应对各种情况的函数。 public static enum PointCut {
SISHEWURU, FLOOR, CEIL, ROUND;
} public static int getIntValue(Object obj, PointCut pointMode) throws NumberFormatException {
try {
Double dblValue = Double.parseDouble(obj.toString());
Double forInt = 0d; int rtnIntVal;
switch(pointMode) {
case SISHEWURU:
forInt = dblValue>=0?Math.floor(dblValue + 0.5):Math.ceil(dblValue - 0.5);
break;
case FLOOR:
forInt = Math.floor(dblValue);
break;
case CEIL:
forInt = Math.ceil(dblValue);
break;
case ROUND:
forInt = dblValue;
break;
}
Long convtedVal = Math.round(forInt);
rtnIntVal = convtedVal.intValue();
return rtnIntVal; } catch (NumberFormatException ex) {
System.out.print("字符串".concat(str).concat("不能转换成整数!"));
throw ex;
}
}