Question calculation of decimals

EagleEye

Member
Joined
Nov 13, 2015
Messages
13
Programming Experience
Beginner
I'm trying to divide a number with a decimal, but i get an error.(DivideByZeroException)

My code is here below and the error is on this line:

res1 = Math.Round(((num3 * num4) / 100) / res, 2);

What am i doing wrong in my calculation?

Here is the numbers i'm typing into my input fields:
num1: 1.07801
num2: 1.06730
num3: 5000
num4: 1
C#:
        private void button1_Click(object sender, EventArgs e)        {
            Decimal num1, num2, num3, num4, res, res1;
            string str1, str2, str3, str4 = null;
            str1 = textBox1.Text;
            str2 = textBox2.Text;
            str3 = textBox3.Text;
            str4 = textBox4.Text;


            if (String.IsNullOrEmpty(str1) || String.IsNullOrEmpty(str2) || String.IsNullOrEmpty(str3) || String.IsNullOrEmpty(str4))
            {
                MessageBox.Show("Please put values in all fields!");
            }
            else
            {
                if (str1.Contains(",") == true || str2.Contains(",") == true || str3.Contains(",") == true || str4.Contains(",") == true)
                {
                    MessageBox.Show("Please Use dots instead og comma!");
                }
                else
                {
                    num1 = Convert.ToDecimal(textBox1.Text);
                    num2 = Convert.ToDecimal(textBox2.Text);
                    res = Math.Round((num1 - num2), 1);
                    label1.Text = Convert.ToString(res) + " Pip";


                    num3 = Convert.ToDecimal(textBox3.Text);
                    num4 = Convert.ToDecimal(textBox4.Text);
                    res1 = Math.Round(((num3 * num4) / 100) / res, 2);
                    label6.Text = Convert.ToString(res1) + " lots";
                }
            }


            if (str1.Contains(",") == true || str2.Contains(",") == true)
                MessageBox.Show("Please use dots instead og comma!");          
        }
 
Last edited:
Back
Top Bottom