int percent = (score/numberOfQuestions)*100;
progressText.setText(score+"/"+numberOfQuestions+" "+percent+"%");
returns 0% no matter what I tired. I tried casting it to int, double, float
Why does it return 0% for a number like score=5 numberOfQuestions=8?
The problem is that divide two integers gives you the integer part of the result. So, (score/numberOfQuestions)
will be always 0.
What you should do is
int percent = (score * 100 / numberOfQuestions);
Then the *100
will be executed first, then the divide will give you correct result.
You need to cast on either of them: -
float percent = ((float)score/numberOfQuestions)*100;
Since, 5 / 10
is already 0.. Casting the final result to any type will give you 0 only, as in below code: -
float percent = ((float)(score/numberOfQuestions))*100;
This will also give you 0.0
. Since you are casting 0
to float. Doesn't matter..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With