0

I have an application that shows one item at a time from a list that they chose from. I want to create a progress bar to show their status of percentage complete.

To do this I think I need 3 numbers:

1.) the number I want to end up with (ie. 100%)

2.) the number of items the user selected

3.) Since the survey I created presents a new item from the list object the user filled with their selection everytime the user clicks the btnNext I need a number that will iterate everytime the hits btnNext

int RAC = Convert.ToInt32(ViewState["count"]) - 2; 
//starts at 0 and goes up to however many selections the user made minus1 or 'countSelection - 1'

With those three numbers here is what I tried

Response.Write(100 / RAC);

but this starts at 100 and goes down to 0. How can I reverse this?

Skullomania
  • 2,225
  • 2
  • 29
  • 65

2 Answers2

3

You only need 2 numbers:

  1. The total number of items (not in percentage). I believe you stated this was: countSelection
  2. The number of items the user has completed. This appears to be: Convert.ToInt32(ViewState["count"]) - 2;

Then you can calculate the percentage completed using the following:

int totalNumberOfItems = countSelection;
int itemsCompleted = Convert.ToInt32(ViewState["count"]) - 2;
int currentProgressPercent = itemsCompleted / totalNumberOfItems * 100;

string displayString = string.Format("{0}/{1} ({2}% complete)", 
    itemsCompleted, totalNumberOfItems, currentProgressPercent);
Rufus L
  • 36,127
  • 5
  • 30
  • 43
1

To make a progress bar all you need are the total number of items and the number of selected items. If you use a min of 0 and a max of 100, the "progress" would then just be:

(100 * selected) / total

Note that you multiple the selected times 100 first, otherwise you would be using integer division for (selected / total) which would be 0 unless selected was equal to total

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Maybe worth noting, that [integer division is not floating point division](http://stackoverflow.com/questions/10851273/why-integer-division-in-c-sharp-returns-an-integer-but-not-a-float). – qqbenq Sep 04 '14 at 17:10
  • @qqbenq That's why I multiply by 100 first. You could cast to double but since the result needs to be an integer this works just as well. – D Stanley Sep 04 '14 at 17:13