2012-10-12 217 views
0

這應該是比較簡單的。顯示進度百分比

什麼我有:

int progress; // iteration we are on, starts at 0, always incr by 1 
int percentage; // from 1 - 100, starts at 0 
int percentage_trip; // derived from total_results/100 
int total_results; // lets say its 200 (note that it can be anything, like 939049) 

我需要:

在上面的例子中,每個時間進展命中2,4,6,8等的百分比整數應該增加1(即百分比++;)

我的想法圍繞着格式化嵌套的IF語句,我是looki這樣做的正確方法。

+3

百分比不應該是'(progress/total_results)* 100'嗎? – Danny

+3

@Danny:整數算術需要'(progress * 100/total_results)'(希望沒有溢出) - 你擁有的總是零,直到progress = total_results。 –

+0

@AustinSalonen true。沒有想到這一點。 – Danny

回答

3
int incrementAmount = total_results/100; 
if (progress % incrementAmount) { percentage++; } 
+0

假設total_results> 200並且不是一些可笑的大數字(或否定的,我們假設它不是),這是行得通的。我不確定模數評估,但是我會在條件語句中添加'== 0'。 如果總數少於200,則必須有一些自定義邏輯以避免奇怪的行爲。 – Devin

+0

通常情況下,total_results將介於20,000 - 60,000之間 –

+0

在這種情況下,這可行。 – Devin