0

I have two variables, I want to showing as percentage, when I calculate them with operator the result is 0 why? please help me. Thanks this is my source

  int count = (from a in dc.jawabans
                         where a.q14 == "5 : Sangat Baik/ Sangat Puas"
                         select a).Count();
            TextBox1.Text = count.ToString();

            int total = (from b in dc.jawabans
                         where b.q14 != ""
                         select b).Count();

            TextBox2.Text = total.ToString();

            int persen = (count / total) * 100;

            TextBox3.Text = persen.ToString();

This is the result

5 Answers5

2

count is int, total is int too. In C# when int divided by int the result is int. The solution is to cast one variable as double.

int persen = (int)((double)count / total * 100);
Niyoko
  • 7,512
  • 4
  • 32
  • 59
1

Write it like this:

decimal persen = (count / (decimal)total) * 100;

After that you can round it if you want:

TextBox3.Text = Math.Round(persen, 2).ToString();

Division of 2 integers is an integer, so you should specified that one of them is decimal.

mybirthname
  • 17,949
  • 3
  • 31
  • 55
  • Do you really need to use `decimal` for something that you're going to display to 2 decimal places? – Matthew Watson Oct 27 '16 at 10:06
  • @MatthewWatson what is your suggestion and why I should not use decimal ? – mybirthname Oct 27 '16 at 10:08
  • Well a `decimal` isn't a build-in intrinsic - it's a struct, and calculations with it are many times slower than using `double` (calculations with which are generally performed in hardware on the processor). `decimal` is intended for calculations - particularly financial calculations - where you need a very high precision. Otherwise, just use `double`. – Matthew Watson Oct 27 '16 at 10:13
  • @MatthewWatson double is also a struct and we don't have background information. This could be currency field which can be reused and really for dividing two numbers you will not have the biggest performance boost. So for current case doesn't matter. – mybirthname Oct 27 '16 at 10:15
  • @mybirthname Generally, processor has special hardware to do `double` computation. `decimal` is not "native" and processor does not have hardware to do the computation. – Niyoko Oct 27 '16 at 10:17
  • @mybirthname A double is nominally a struct, but as I said it's also a built-in intrinsic for which the processor has native support. Therefore you should always use a `double` instead of a `decimal` unless there is a specific reason not to. (It's similar to using `int` instead of `BigInteger`.) – Matthew Watson Oct 27 '16 at 11:28
0

Because you dividing two integers, so the result will be integer as well. You can set count and total as double , then you will get correct result.

luki
  • 72
  • 2
0

This is because the sum you are doing is with ints, so the value is rounded to the nearest whole number - for example if count is 20, and total is 100

int persen = (count / total) * 100;

is the same as doing

int persen = (count / total); //this = 0 as it would evaluate to 0.2 => 0
persen = persen * 100; //still 0

Whereas

int persen = ((double)count / (double)total) * 100;
//This would be 20, as count and total are both cast to a double - it also works if you only cast one of them
Alfie Goodacre
  • 2,753
  • 1
  • 13
  • 26
0
decimal persen = (count / (decimal)total) * 100; //count 20, total 100, so person will be 0 if it is int in your code

If you devide int by int, it will give you int not double. So either convert count or total as decimal or double according to your requirement.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197