2011-09-16 23 views
59

我剛剛寫了一個微小的方法來計算手機短信的頁數。我沒有選擇使用Math.ceil,誠實地說,它似乎是非常醜陋的。如何將整數除法取整並在Java中產生int結果?

這裏是我的代碼:

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); 
     } 
    } 
} 

我真的不喜歡這段代碼和我正在尋找這樣做的更優雅的方式。有了這個,我期待3而不是3.0000000。有任何想法嗎?

+1

[如何舍入整數分割的結果](http:// stackoverflow。com/questions/17944/how-to-round-up-integer-division的結果) – Raedwald

回答

79

圍捕,您可以使用

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; 
} 
+0

請注意,這隻適用於'num> = 0'。 – user905686

+0

它輪到正無限。它不會從零開始,這是另一種選擇。 –

+2

我的意思是,嘗試'num = -2'和'div = -3'。 這會以'-6/-3 = 2'結尾,但'0,666..'應該舍入爲'1'。 實際上它不適用於'num <= 0 && div <= 0'。 – user905686

10
(message.length() + 152)/153 

這將給出一個「向上取整」的整數。

0

,這可能是有幫助,, 減去剩餘的legnth,並使其成爲可除數然後將其除以153

int r=message.length()%153;  //Calculate the remainder by %153 
return (message.length()-r)/153; // find the pages by adding the remainder and 
            //then divide by 153 
1

Ex panding彼得的解決方案,這是我發現我總是圓「向正無窮大的作品:

public static long divideAndRoundUp(long num, long divisor) { 
    if (num == 0 || divisor == 0) { return 0; } 

    int sign = (num > 0 ? 1 : -1) * (divisor > 0 ? 1 : -1); 

    if (sign > 0) { 
     return (num + divisor - 1)/divisor; 
    } 
    else { 
     return (num/divisor); 
    } 
} 
+0

除以零不爲零,但應該是錯誤的。 –

95

使用Math.ceil()並把結果爲int:

  • 這依然較快而不是通過使用abs()來避免雙打。用底片工作時
  • 結果是正確的,因爲-0.999將被舍入到0

實施例:

(int) Math.ceil((double)divident/divisor); 
+3

這應該是正確的答案 – Duane

+1

@Duane OP說:「我沒有選擇使用Math.ceil湊成」 – Hemmels

+0

公平點,雖然我認爲這是谷歌現在頂級的「數學四捨五入」命中:) 。羞愧它有這樣的條件。 – Duane

6
long numberOfPages = new BigDecimal(resultsSize).divide(new BigDecimal(pageSize), RoundingMode.UP).longValue(); 
+0

+1使用內置功能 –

21

另一單行,是不是太複雜:

private int countNumberOfPages(int numberOfObjects, int pageSize) { 
    return numberOfObjects/pageSize + (numberOfObjects % pageSize == 0 ? 0 : 1); 
} 

可以使用long而不是int;只需更改參數類型和返回類型。

+2

這應該是答案。這可能是最簡單的方法來實現,並避免不執行任何額外的不必要的步驟。它還可以避免在投射到不同的數字類型時出現輕微的附加問題。 –

+0

好的解決方案。謝謝 – ghui

1

如果要計算除以b圍捕你可以使用(A +( - A%B))/ B

0

如果要導入什麼都沒有,我的建議是:

int var = message.length()/153; //you can put any integer instead of 153 depend on your case 
if(message.length() % 153 != 0) 
    var = var + 1; 
8

谷歌的番石榴庫handles this in the IntMath class

IntMath.divide(numerator, divisor, RoundingMode.CEILING); 

不像這裏的許多答案,它處理負數。它在嘗試除以零時也會引發適當的異常。

相關問題