SDK and simple math - I know, I'm stupid

Discussion in 'iPhone/iPad Programming' started by mcannell, Dec 30, 2008.

  1. macrumors member

    Joined:
    Dec 20, 2008
    #1
    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
     
  2. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #2
    The (4/10) is going to result in (int)4/(int)10, ie. 0.

    Change it to (4./10.)*100.
     
  3. thread starter macrumors member

    Joined:
    Dec 20, 2008
    #3
    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
     
  4. macrumors 603

    swiftaw

    Joined:
    Jan 31, 2005
    Location:
    Omaha, NE, USA
    #4
    what is you replace myscore by myscore*1.0, that might make the output a float rather than an integer.
     
  5. macrumors regular

    Joined:
    Jun 23, 2006
    Location:
    Glasgow, UK
    #5
    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).
     
  6. thread starter macrumors member

    Joined:
    Dec 20, 2008
    #6
    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;
     
  7. thread starter macrumors member

    Joined:
    Dec 20, 2008
    #7
    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
     
  8. thread starter macrumors member

    Joined:
    Dec 20, 2008
    #8
    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;
     
  9. macrumors 6502a

    Joined:
    Sep 14, 2007
    Location:
    Manchester, UK
    #9
    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.
     

Share This Page