Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mcannell

macrumors member
Original poster
Dec 20, 2008
36
0
I know this is stupid... very stupid....

I just want to display a percentage. How simple this should be, but my output is always 0


Code:
double actualPercentage = (4/10)*100;
NSString *finalPercent = [NSString stringWithFormat:@"%d",actualPercentage];
finalPercent = [finalPercent stringByAppendingString:@"%"];
scorePercent.text = finalPercent;

I've tried NSInteger, NSNumber, %d, %f%g

I've searched all over the internet and can't seem to find info about something so elementary

i would expect to see 40% as my output
 

plinden

macrumors 601
Apr 8, 2004
4,029
142
The (4/10) is going to result in (int)4/(int)10, ie. 0.

Change it to (4./10.)*100.
 

mcannell

macrumors member
Original poster
Dec 20, 2008
36
0
OK

my code is actually like this, how does it change what you said

Code:
double actualPercentage = (myScore/numberOfQuestions)*100;

I assume I can't just put a . after each variable

come to think of it, the myScore & numberOfQuestions are declared as NSInteger
 

russellelly

macrumors regular
Jun 23, 2006
139
41
Glasgow, UK
OK

my code is actually like this, how does it change what you said

Code:
double actualPercentage = (myScore/numberOfQuestions)*100;

I assume I can't just put a . after each variable

come to think of it, the myScore & numberOfQuestions are declared as NSInteger

Code:
double actualPercentage = ((double)myScore/(double)numberOfQuestions) * 100.0

should do the trick (in fact the 100 probably doesn't need a .0, but it wouldn't do any harm).
 

mcannell

macrumors member
Original poster
Dec 20, 2008
36
0
I still get 0%

I verified that I am getting proper values for myScore & numberOfQuestions

Code:
//Display percent score in 80% format
double actualPercentage = ((double)myScore/(double)numberOfQuestions)*100.0;

NSString *finalPercent = [NSString stringWithFormat:@"%d",actualPercentage];
finalPercent = [finalPercent stringByAppendingString:@"%"];
	
scorePercent.text = finalPercent;
 

mcannell

macrumors member
Original poster
Dec 20, 2008
36
0
ok, I changed %d to %lf and it works but instead of say 40% I get 40.0... then it runs out of space. So how do I drop all the stuff at the decimal and get 40%

Maybe double is the wrong thing
 

mcannell

macrumors member
Original poster
Dec 20, 2008
36
0
I got it.


I changed to float and that gave me the same output as double.
So I changed the specifier to %.0f and it works

so now it looks like this

Code:
//Display percent score in 80% format
float actualPercentage = ((float)myScore/(float)numberOfQuestions)*100.0;

NSString *finalPercent = [NSString stringWithFormat:@"%.0f",actualPercentage];
finalPercent = [finalPercent stringByAppendingString:@"%"];
	
scorePercent.text = finalPercent;
 

TonyHoyle

macrumors 6502a
Sep 14, 2007
999
0
Manchester, UK
You could just do the multiplication first:

(myScore*100)/numberOfQuestions

This lets you stay with integers. It's a trick worth remembering if you have to do more complex calculations, especially in time critical loops.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.