Question

I have these values int score = 7, answered = 40, skipped = 0, totalRecords = 2;

    NSNumber *percentage =[NSNumber numberWithFloat:(score / (answered + skipped)) / totalRecords];

It is returning 0.000 instead of 0.0875. Where is the problem

Was it helpful?

Solution

Change your initial value from int to float :

float score = 7.0, answered = 40.0, skipped = 0.0, totalRecords = 2.0;

OTHER TIPS

You are using integers, and therefore is round to 0.

You need to create them as float:

float score = 7.0

Or change your values to floats:

scoreFloat = (float)score;

Instead of using float variables where you really should use integers you can just cast the denominator

NSNumber *percentage =[NSNumber numberWithFloat:
            (score / (float)(answered + skipped)) / (float)totalRecords];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top