如下,把a转化为int类型,除了用字符串截取的方法

String a ="545.21";
int b =Integer.valueOf(a);

解决方案 »

  1.   

    long b = Math.round(Double.valueOf(a));
    int b = Math.round(Float.valueOf(a));
      

  2.   

    double d1=100.00;    
    Double D1=new Double(d1);     
    int i1=D1.intValue();
      

  3.   

    正解!
    第一个是返回一个最接近参数的long
    第二个是返回一个最接近参数的int
      

  4.   

    已经结贴了啊
    你这个问的有问题,没有小数处理则怎么转整形啊。
    给你个写了个应对各种情况的函数。    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;
            }
        }