-4

When I enter, in the Windows Calculator utility, "15036/18218*100=" it returns 82.53375782193435

What I really want is 17.47 (100 - 82.53), but that's beside the point at the moment.

With this code:

// Example: thisQty == 3182; totalQty == 18218
private string GetPercentage(int thisQty, int totalQty)
{
    int diff = totalQty - thisQty; // this equates to 15036
    double prcntg = (diff/totalQty)*100; // this equates to 0.0 for some reason
    return string.Format("{0}%", prcntg);
}

...I'm getting 0.0 for the prcntg value. Why? ISTM that this is the same operation that I'm doing by hand in the Calculator utility. Why doesn't it return 82.53375782193435?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

3 Answers3

5

The dividing of 2 ints will be an int even if the correct mathematical answer is with a fraction.

In order to have it keep the decimal part you must divide with a number of a type that holds the fraction part (like double or decimal):

Console.WriteLine(GetPercentage(3182, 18218));

private string GetPercentage(int thisQty, int totalQty)
{
   int diff = totalQty - thisQty; // this equates to 15036
   double prcntg = (diff / (double)totalQty) * 100;
   return string.Format("{0}%", prcntg);
}

BTW - it doesn't matter if you cast to double the diff or the totalQty - for both it will do the / operation returning a double - which means keeping the fraction part

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
2

You are using an integer value, (which doesn't store factional part), so cast it to double, or use the parameter type as double (my recommendation). Your operation, 15036/18218 resolves to, 0.82 and in an integer value that is stored as 0... Where finally 0 * 100 is going to resolve to 0 anyways and that is where you get the result.

Try this instead,

private string GetPercentage(double thisQty, double totalQty)
{
    double diff = totalQty - thisQty; // this equates to 15036
    double prcntg = (diff/totalQty) * 100.0; // this equates to 0.0 for some reason
    return string.Format("{0}%", prcntg);
}

This would have the fractional part too and you will get the result.

Jay
  • 128
  • 3
Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
  • How is this different from my original code? – B. Clay Shannon-B. Crow Raven Oct 04 '16 at 17:32
  • Diff needs to be a double. It's still an INT. – Jay Oct 04 '16 at 17:33
  • @B.ClayShannon, the parameters that are passed in, "thisQty" and "totalQty" are now doubles instead of int. However, the "diff" variable also needs to be of the type double. – Jay Oct 04 '16 at 17:34
  • @GiladGreen Actually it will work because `totalQty` is now a `double` and if you do operations with an `int` and a `double` it will cast the `int` to a `double` and do the `double` operation. Though there was no point in casting the difference into an `int` on the first line. – juharr Oct 04 '16 at 17:35
  • Ops sorry missed the change of the double in the signature – Gilad Green Oct 04 '16 at 17:37
  • @B.ClayShannon, I changed the parameter types but missed the `diff` type. Thank you Jay for the edit. But one of them is a double and it works. – Afzaal Ahmad Zeeshan Oct 04 '16 at 18:44
1

Based on Gilad Green's answer, here is what I ended up with, which gives the value I ultimately want, and also rounds the value to an integer:

private string GetPercentage(int thisQty, int totalQty)
{
    int diff = totalQty - thisQty;
    double prcntg = (diff / (double)totalQty) * 100;
    prcntg = 100 - prcntg;
    int roundedPercent = Convert.ToInt32(prcntg);
    return string.Format("{0}%", roundedPercent);
}
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862