0

I have a quiz game and i wanna show the percentage of level and maxlevel.

  • (levelvideo) is my first integer of current level
  • (QuestionLibraryVideo.mChoices.length) is my max level from length of table..

I try to show this = (levelvideo/QuestionLibraryVideo.mChoices.length)*100 but shows me only zero.

thsekato2.setText(Integer.toString((levelvideo/QuestionLibraryVideo.mChoices.length)*100));
andand
  • 17,134
  • 11
  • 53
  • 79
Marios
  • 5
  • 7
  • You are dividing two integers which will yield an integer result. Try casting one of your values to a double – Katajun Feb 22 '18 at 15:45
  • 3
    Possible duplicate of [Division of integers in Java](https://stackoverflow.com/questions/7220681/division-of-integers-in-java) – Selvin Feb 22 '18 at 15:54

2 Answers2

0

IF you divide integer by integer, you will loose decimals after ,.
For example 3/2 is 1, beacuse it's 1,5, so integer have only 1.
Use float or double instead.

 float f1 = levelvideo;
 float f2 = QuestionLibraryVideo.mChoices.length;
 zero.thsekato2.setText(Integer.toString((int)((f1/f2)*100)));
J K
  • 632
  • 7
  • 24
0

If you want to do this only with integer arithmetic, you can multiply the numerator by 100 before dividing. It doesn't have the precision offered by converting to a double, but then, it doesn't have the overhead of type casting either. This would look something like:

thsekato2.setText(Integer.toString((100*levelvideo)/QuestionLibraryVideo.mChoices.length));
andand
  • 17,134
  • 11
  • 53
  • 79