none
Handling Divide By Zero exception always returns 0 RRS feed

  • Question

  • Dear Experts,

    I have to get the decimal value by using expression, where values are passed from 3 fields. To avoid divide by zero error, I'm checking if the value exists before using the expression. 

    But when the value exists in the dividend field, the result always returns zero.

     public DivideByZeroForm()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                int startnum = int.Parse(textBox1.Text);
                int middlenum = int.Parse(textBox2.Text);
                int dead = int.Parse(textBox3.Text);
                decimal val ;
                if (startnum != 0)
                {
                    val = (middlenum - dead) / startnum *100;
                    textBox4.Text = val.ToString();
                }
                else textBox4.Text = "00.0";
            }

     Thanks for the guidance.

    NewB


    NewBInVB

    Thursday, March 1, 2018 2:43 PM

Answers

  • Have you tried it with various values? Depending on the values, sure it's possible to have 0 come back.

    I tried this with

    static public class NumericExtensions
    {
        static public decimal SafeDivision(
            this decimal Numerator, decimal Denominator) => (Denominator == 0) ? 
            0 : Numerator / Denominator;
    }

    And

    private void button1_Click(object sender, EventArgs e)
    {
        if (int.TryParse(textBox1.Text, out var value1) && int.TryParse(textBox2.Text, out var value2) && int.TryParse(textBox3.Text, out var value3))
        {
            var SampleResult1 = NumericExtensions.SafeDivision(value1, value2);
            if (SampleResult1 != 0)
            {
                var result = (value2 - value3) / value1 * 100;
                MessageBox.Show(result.ToString("00.0"));
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by NewBInCoding Friday, March 2, 2018 7:34 AM
    Thursday, March 1, 2018 3:15 PM
    Moderator
  • Hi NewBInVB,

    >>But when the value exists in the dividend field, the result always returns zero.

    That is due to the following code:

    val = (middlenum - dead) / startnum *100;

    Because you set the "middlenum", "dead", "startnum" to int type, so it does not keep the decimal point during the operation

    For example, "middlenum"=21, "dead"=20, "startnum"=10, if you set them to 'int', then the result "middlenum - dead"=1, "(middlenum - dead) / startnum"=0 instead of 0.1

    So you can change your code to:

            private void button1_Click(object sender, EventArgs e)
            {
                double startnum = double.Parse(textBox1.Text);
                double middlenum = double.Parse(textBox2.Text);
                double dead = double.Parse(textBox3.Text);
                double val;
                decimal val2;
                if (startnum != 0)
                {
                    val = (middlenum - dead) / startnum * 100;
                    val2 = (decimal)val;
                    textBox4.Text = val2.ToString();
                }
                else textBox4.Text = "00.0";
            }

    Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Stanly Fan Friday, March 2, 2018 6:07 AM
    • Marked as answer by NewBInCoding Friday, March 2, 2018 7:34 AM
    Friday, March 2, 2018 6:04 AM

All replies

  • Have you tried it with various values? Depending on the values, sure it's possible to have 0 come back.

    I tried this with

    static public class NumericExtensions
    {
        static public decimal SafeDivision(
            this decimal Numerator, decimal Denominator) => (Denominator == 0) ? 
            0 : Numerator / Denominator;
    }

    And

    private void button1_Click(object sender, EventArgs e)
    {
        if (int.TryParse(textBox1.Text, out var value1) && int.TryParse(textBox2.Text, out var value2) && int.TryParse(textBox3.Text, out var value3))
        {
            var SampleResult1 = NumericExtensions.SafeDivision(value1, value2);
            if (SampleResult1 != 0)
            {
                var result = (value2 - value3) / value1 * 100;
                MessageBox.Show(result.ToString("00.0"));
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by NewBInCoding Friday, March 2, 2018 7:34 AM
    Thursday, March 1, 2018 3:15 PM
    Moderator
  • Step through in the debugger.

    And don't forget that if middlenum = dead then middlenum - dead = 0; and 0 divided by anything = 0

    :)

    Thursday, March 1, 2018 3:20 PM
  • Maybe try this too:

       val = (middlenum - dead) *100 / startnum;

    Thursday, March 1, 2018 6:46 PM
  • Hi NewBInVB,

    >>But when the value exists in the dividend field, the result always returns zero.

    That is due to the following code:

    val = (middlenum - dead) / startnum *100;

    Because you set the "middlenum", "dead", "startnum" to int type, so it does not keep the decimal point during the operation

    For example, "middlenum"=21, "dead"=20, "startnum"=10, if you set them to 'int', then the result "middlenum - dead"=1, "(middlenum - dead) / startnum"=0 instead of 0.1

    So you can change your code to:

            private void button1_Click(object sender, EventArgs e)
            {
                double startnum = double.Parse(textBox1.Text);
                double middlenum = double.Parse(textBox2.Text);
                double dead = double.Parse(textBox3.Text);
                double val;
                decimal val2;
                if (startnum != 0)
                {
                    val = (middlenum - dead) / startnum * 100;
                    val2 = (decimal)val;
                    textBox4.Text = val2.ToString();
                }
                else textBox4.Text = "00.0";
            }

    Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Stanly Fan Friday, March 2, 2018 6:07 AM
    • Marked as answer by NewBInCoding Friday, March 2, 2018 7:34 AM
    Friday, March 2, 2018 6:04 AM
  • Thank you Frankie!

    NewBInVB

    Friday, March 2, 2018 7:35 AM
  • Thank you for the guidance Karen.


    NewBInVB

    Friday, March 2, 2018 7:36 AM