• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

Percentage in C#

Hello everyone,

I have a survey that is using the Silverlight graph to show the responses. The problem I'm having is that it doesn't show the percentage. It only shows the number of response. I was working a code to create the percentage but the results keep coming up as zero. It says I'm trying to divide by zero which it is not the case because there are responses for each question in a survey.

Is there something I'm doing wrong in the following or a better way to do it in a case statement:

 private void GetQuestionResponses()
        {
            
            List<string> unParsedResults = new List<string>();
            int totalCnt1 = 0;
            int totalCnt2 = 0;
            int totalCnt3 = 0;
            int Cnt2resp1 = 0;
            int Cnt2resp2 = 0;
            int Cnt2resp3 = 0;
            int Cnt2resp4 = 0;
            int Cnt2resp5 = 0;
            foreach (ListItem li in ReturnItems)
            {
                if (CurrentQuestion.Key.ToString() == "90 Day New Employee")
                {                    
                    totalCnt1++;
                    MessageBox.Show("90 Day New Employee count is: " + totalCnt1);
                    string tempResult = li[CurrentQuestion.Value] == null ? string.Empty : li[CurrentQuestion.Value].ToString();                 
                    unParsedResults.Add(tempResult);
                }
                else if (CurrentQuestion.Key.ToString() == "1 Year New Employee Questionnaire")
                {
                    totalCnt2++;
                    MessageBox.Show("1 Year New Employee Questionnaire count is: " + totalCnt2);
                    string tempResult = li[CurrentQuestion.Value] == null ? string.Empty : li[CurrentQuestion.Value].ToString();
                    if (tempResult.ToString() == "Strongly Agree")
                    {
                        Cnt2resp1++;
                    }
                    else if (tempResult.ToString() == "Agree")
                    {
                        Cnt2resp2++;
                    }
                    else if (tempResult.ToString() == "Neither agree nor disagree")
                    {
                        Cnt2resp3++;
                    }
                    else if (tempResult.ToString() == "Disagree")
                    {
                        Cnt2resp4++;
                    }
                    else if (tempResult.ToString() == "Strongly disagree")
                    {
                        Cnt2resp5++;
                    }
                    unParsedResults.Add(tempResult);
                }
                else// if (CurrentQuestion.Key.ToString() == "First Week New Employee")
                {
                    totalCnt3++;
                    MessageBox.Show("First Week New Employee count is: " + totalCnt3);
                    string tempResult = li[CurrentQuestion.Value] == null ? string.Empty : li[CurrentQuestion.Value].ToString();
                    unParsedResults.Add(tempResult);
                }  
               // MessageBox.Show("Result: " + tempResult);                
            }
            //isoguMessageBox.Show("Current Q: "+CurrentQuestion.Key.ToString());

            AddToSurveyResults(CurrentQuestion.Key, unParsedResults);

            // Get responses for the next question, if one exists
            if (CurrentQuestionIndex + 1 < QuestionsToChart.Length)
            {
                CurrentQuestionIndex++;
                QuerySurveyList();
            }
            else
            {
                // No more questions to chart, so bind the data to the chart
                BindResultsToChart();
            }

           MessageBox.Show("total cnt2: " + totalCnt2);
            int perCnt2 = (Cnt2resp3 / totalCnt2) * 100;
             MessageBox.Show("agree nor disagree: " + perCnt2);

        }

        

Open in new window

0
Isaac
Asked:
Isaac
2 Solutions
 
djon2003Commented:
You shall cast your numbers to float.

int perCnt2 = ((float)Cnt2resp3 / (float) totalCnt2) * (float)100;

Though, about the division by zero. As I see, I presume the totalCnt2 is not zero, so I don't know what's this.
0
 
OrcbighterCommented:
your logic increments totalCnt2 only if one condition is met:
 CurrentQuestion.Key.ToString() == "1 Year New Employee Questionnaire"

if not, it remains zero and your end up dividing by zero.
At the very least you should test for zero before using it as a divisor.

Have you confirmed that totalCnt2 is not zero ?
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now