Percentage in C#

Posted on 2011-10-31
Last Modified: 2012-08-13
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")
                    MessageBox.Show("90 Day New Employee count is: " + totalCnt1);
                    string tempResult = li[CurrentQuestion.Value] == null ? string.Empty : li[CurrentQuestion.Value].ToString();                 
                else if (CurrentQuestion.Key.ToString() == "1 Year New Employee Questionnaire")
                    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")
                    else if (tempResult.ToString() == "Agree")
                    else if (tempResult.ToString() == "Neither agree nor disagree")
                    else if (tempResult.ToString() == "Disagree")
                    else if (tempResult.ToString() == "Strongly disagree")
                else// if (CurrentQuestion.Key.ToString() == "First Week New Employee")
                    MessageBox.Show("First Week New Employee count is: " + totalCnt3);
                    string tempResult = li[CurrentQuestion.Value] == null ? string.Empty : li[CurrentQuestion.Value].ToString();
               // 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)
                // No more questions to chart, so bind the data to the chart

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



Open in new window

Question by:Isaac
    LVL 9

    Accepted Solution

    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.
    LVL 9

    Assisted Solution

    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 ?

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now