2011-04-22 104 views
3

請看看下面的代碼,並告訴我,爲什麼skillPercentange始終爲零:簡單percentange計算

NSInteger skillPercentange; 
skillPercentange=totalCorrectAns * reloader/100; 
+0

'totalCorrectAns'和'reloader'的值是多少? – pavium 2011-04-22 12:06:38

+0

告訴我們關於totalCorrectAns和reloader,或許一些更多的核心..!? – Vassilis 2011-04-22 12:07:40

回答

11

我並不確切地知道你想要做什麼,但我會盡力給你看演示:

int allQuestions = 50; 
int correctAnswers = 25; 
float precentage = (100 * correctAnswers)/allQuestions; 
NSLog("%i of %i questions answered correctly. That are %.1f percent!",correctAnswers,allQuestions,precentage); 

輸出: 「50個問題25回答正確即50.0%的!」

+0

對不起我的愚蠢的錯誤,並感謝您的幫助 – 2011-04-22 12:34:15

+0

如果我做10/100,他們都是浮動,我得到0.1000001 ...怪異.. – 2015-09-14 20:48:27

+0

@FranciscoCorralesMorales正確。儘管這聽起來真的很奇怪,但是你的設備不能計算10/100而不失去一些準確性。 – 2015-09-15 03:49:44

0
NSInteger skillPercentange; 
skillPercentange = 100 * totalCorrectAns/reloader; 
1
float skillPercentange; 
skillPercentange=(totalCorrectAns * reloader)/100; 

skillPercentange= totalCorrectAns * (reloader/100); 

根據什麼是你的公式。

NSLog(@"skillPercentange=0.1f",skillPercentange);