Division

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have the following:

V = A / B * 100

V, A and B are integers. How can I be sure that the result would be an
integer.

And how can I assign a value to V in case B=0 creating a division by
0.

Thanks,
Miguel
 
shapper said:
I have the following:

V = A / B * 100

V, A and B are integers. How can I be sure that the result would be an
integer.

It always will be, because that's how division works in C#. If you mean
you need to check that A really is divisible by B, use %:

if ((A % B) != 0)
{
throw new ArgumentException(...);
}
And how can I assign a value to V in case B=0 creating a division by
0.

V = B == 0 ? WhateverValueYouWant : A/B * 100;
 
How can I be sure that the result would be an integer.


Are you concerned about overflow? is that the case, one option would
be to use:
int V = checked(A / B * 100);

And how can I assign a value to V in case B=0 creating a division by.

I have a feeling I am not understanding what you are asking and that
the following is not the right answer but here is a best guess:

int V;
if(B == 0)
{
V = 0;
}
else
{
V = checked(A / B * 100);
}
 
Back
Top