## 如何将整数除法归并在Java中得到整数除法的结果？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (13)

```public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String message = "today we stumbled upon a huge performance leak while optimizing a raycasting algorithm. Much to our surprise, the Math.floor() method took almost half of the calculation time: 3 floor operations took the same amount of time as one trilinear interpolation. Since we could not belive that the floor-method could produce such a enourmous overhead, we wrote a small test program that reproduce";

System.out.printf("COunt is %d ",(int)messagePageCount(message));

}

public static double messagePageCount(String message){
if(message.trim().isEmpty() || message.trim().length() == 0){
return 0;
} else{
if(message.length() <= 160){
return 1;
} else {
return Math.ceil((double)message.length()/153);
}
}
}```

### 2 个回答

``````import static java.lang.Math.abs;

public static long roundUp(long num, long divisor) {
int sign = (num > 0 ? 1 : -1) * (divisor > 0 ? 1 : -1);
return sign * (abs(num) + abs(divisor) - 1) / abs(divisor);
}
``````

``````public static long roundUp(long num, long divisor) {
return (num + divisor - 1) / divisor;
}
``````

• 这仍然比使用abs()避免双倍的速度要快。
• 当使用负片时，结果是正确的，因为-0.999将被舍入到0。

`(int) Math.ceil((double)divident / divisor);`