Better way to calculate the score without floating point

I prefer to avoid floating point if I can. As long as the numbers stay small enough and you don’t need to show decimal fractions, you can use integers just fine. Making this change avoids having to use the math package as was done in the lesson (and integer calculations are faster, although not an issue here):

public int CalculateScore()
{
    return (correctAnswers * 100) / questionsSeen;
}   // CalculateScore()

Note that this will always round down to the lowest integer value, so if you need different rounding (like to nearest even or to have it round up use floating point).

4 Likes

Privacy & Terms