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:
Debug your 'res' value and see what it is, then you should understand why dividing by this value gives you DivideByZeroException.
 
Debug your 'res' value and see what it is, then you should understand why dividing by this value gives you DivideByZeroException.

Thanks John,

'res' was zero and its seems that the problem was that i forgot to multiply res with 10000 before the Math.Round
 
I have a strange problem though.

when i'm testing it on my own computer where i have Visual Studio installed, it works fine.
Results:
107.1 Pip
0.47 lots

When i run it on another computer the results are.
10710000 Pips
0.00 lots

Is it something in the code i have done incorrect?
 
I have a strange problem though.

when i'm testing it on my own computer where i have Visual Studio installed, it works fine.
Results:
107.1 Pip
0.47 lots

When i run it on another computer the results are.
10710000 Pips
0.00 lots

Is it something in the code i have done incorrect?
Are you using the same exe file on both machines? For such a simple math calculation it's the only thing I can think of without knowing more about your program.
 
I have tried to copy the .exe file to a USB key and start it from there, but still i get two different results from the same .exe file.

Could someone please try to download it from my website to check if it show the correct numbers?

ForumPicture.jpg

Link: Traders Corner
 
Last edited:
Your screenshots show you are using different versions.
 
When I run it and enter the values that you have I'm getting the same output as in the Win10 screenshot (the one on the left).
But I also am in the US, the Win7 screenshot (right side) looks to be set to a locality that is not US.
 
When I run it and enter the values that you have I'm getting the same output as in the Win10 screenshot (the one on the left).
But I also am in the US, the Win7 screenshot (right side) looks to be set to a locality that is not US.

Ok, so it's the local settings that gives the problem.

How can i solve this?
 
Back
Top Bottom