SkylaurRoe
SkylaurRoe

Reputation: 35

Allow for divide by zero to = zero

I know that you cannot divide anything by zero.

But, is it possible to have visual studio to give a result of zero if any arithmetic operation attempts to divide by zero?

if so, how?

for this particular project, it would actually save me a lot of work and code if I could allow anything that is / 0 = 0

I am looking to have the result of an integer divided by zero to equal zero. VB.NET Visual Studio 2010.

Upvotes: 1

Views: 3331

Answers (3)

jace
jace

Reputation: 1674

Why not using if in a static method if you want to access it in all computation?

C#/Java code

public static int myDivision(int dividend, int divisor)
{
    //return is the answer

    if(divisor == 0)
    { return 0; }
    else
    { return dividend/divisor; }

    //just do something like this
}

or for something clearer

public static int myDivision(int dividend, int divisor)
{
    int quotient = 0;

    if(divisor == 0)
    { return quotient; }
    else
    { 
      quotient = dividend/divisor;
      return quotient; 
    }

    //just do something like this
}

//note: you may also use it through an object method

Upvotes: 1

I think the best solution is simply by checking the divisor of the division with an if statement. You can decide to perform the division if the divisor is not 0.

Upvotes: 2

Jason P Sallinger
Jason P Sallinger

Reputation: 226

Try Catch

Catch the exception Divide By Zero

Do as you will.

Upvotes: 1

Related Questions