View Full Version : SDK and simple math - I know, I'm stupid
mcannell
Dec 31, 2008, 01:08 AM
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
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
Dec 31, 2008, 01:28 AM
The (4/10) is going to result in (int)4/(int)10, ie. 0.
Change it to (4./10.)*100.
mcannell
Dec 31, 2008, 02:51 AM
OK
my code is actually like this, how does it change what you said
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
swiftaw
Dec 31, 2008, 02:54 AM
what is you replace myscore by myscore*1.0, that might make the output a float rather than an integer.
russellelly
Dec 31, 2008, 04:27 AM
OK
my code is actually like this, how does it change what you said
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
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
Dec 31, 2008, 08:33 AM
I still get 0%
I verified that I am getting proper values for myScore & numberOfQuestions
//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
Dec 31, 2008, 08:38 AM
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
Dec 31, 2008, 08:53 AM
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
//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
Dec 31, 2008, 10:29 AM
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.
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.