Always Round UP a Double

前端 未结 7 1701
温柔的废话
温柔的废话 2020-12-06 09:55

How could I always round up a double to an int, and never round it down. I know of Math.round(double), but I want it to always round u

7条回答
  •  半阙折子戏
    2020-12-06 10:03

    private int roundUP(double d){
        double dAbs = Math.abs(d);
        int i = (int) dAbs;
        double result = dAbs - (double) i;
        if(result==0.0){ 
            return (int) d;
        }else{
            return (int) d<0 ? -(i+1) : i+1;          
        }
    }
    

    Good job ! ;)

提交回复
热议问题