round a floating-point number to the next integer value in java

后端 未结 6 1369
广开言路
广开言路 2020-12-29 18:08

how can i round up a floating point number to the next integer value in Java? Suppose

2.1 -->3

3.001 -->4

4.5 -

6条回答
  •  情深已故
    2020-12-29 18:40

    I had the same issue where I was still getting the smaller int value. It was the division, not the Math.ceil. You have to add a (float) cast to the ints. This is how I fixed it:

    int totalNumberOfCachedData = 201;
    int DataCountMax = 200;
    
    float ceil =(float) totalNumberOfCachedData / (float)DataCountMax;
    int roundInt = (int) Math.ceil(ceil);
    

    This will give me 2 for the value of roundInt.

提交回复
热议问题