Search code examples
c#asp.netinteger-division

making double variables lower than 1


in my ASP.NET project i did a survey page that uses Application to save the votes. I have a problem with the making of the percentages amount. I've tried many things. here is the problematic part of my code:

    double x = (count / sum) ;
    double f = (count1 / sum) ;
    double g = (count2 / sum) ;
    double h = (count3 / sum) ;
    if (sum > 0)
    {
        a = (int)x * 100;
        b = (int)f * 100;
        c = (int)g * 100;
        d = (int)h * 100;
    }

I used breakpoints and figured out that the problem was in the double variables: the (count/sum) equals 0 anyway.


Solution

  • I'm assuming count and sum are integer types.

    The result of division of 2 integers is a truncated integer.

    You need to cast one side of the division to a double, then the result will be double

    So

    ((double)count)/sum