division with wrong result
Posted
by
PeterK
on Stack Overflow
See other posts from Stack Overflow
or by PeterK
Published on 2011-01-09T15:47:33Z
Indexed on
2011/01/09
15:53 UTC
Read the original article
Hit count: 282
Hi,
I am trying to divide integers but get 0 as result. I just do not understand what i am doing wrong. I am using only int's in this example but get the same result testing with float or double.
The code i use is:
int wrongAnswers = askedQuestions - playerResult;
int percentCorrect = (playerResult / askedQuestions) * 100;
int percentWrong = (wrongAnswers / askedQuestions) * 100;
NSLog(@"askedQuestions: %i", askedQuestions);
NSLog(@"playerResult: %i", playerResult);
NSLog(@"wrongAnswers: %i", wrongAnswers);
NSLog(@"percentCorrect: %i", percentCorrect);
NSLog(@"percentWrong: %i", percentWrong);
NSLog(@"calc: %i", (wrongAnswers + playerResult));
NSLog(@"wrong answers %: %i %%", ((wrongAnswers / askedQuestions) * 100));
The result i get is:
2011-01-09 16:45:53.411 XX[8296:207] askedQuestions: 5
2011-01-09 16:45:53.412 XX[8296:207] playerResult: 2
2011-01-09 16:45:53.412 XX[8296:207] wrongAnswers: 3
2011-01-09 16:45:53.413 XX[8296:207] percentCorrect: 0 %
2011-01-09 16:45:53.414 XX[8296:207] percentWrong: 0 %
2011-01-09 16:45:53.414 XX[8296:207] calc: 5
2011-01-09 16:45:53.415 XX[8296:207] wrong answers : 0 %
I would very much appreciate help :-)
© Stack Overflow or respective owner