# Questioncalculation of decimals

#### EagleEye

##### Member
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)
{
}
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)
}``````

Last edited:

#### JohnH

##### C# Forum Moderator
Staff member
Debug your 'res' value and see what it is, then you should understand why dividing by this value gives you DivideByZeroException.

#### EagleEye

##### Member
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

#### EagleEye

##### Member
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?

#### JuggaloBrotha

Staff member
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.

#### EagleEye

##### Member
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.

Thanks Juggallo,

I don't think the .exe files are diffent, but I will check up on that.

#### EagleEye

##### Member
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.

Thanks Juggalo,

I don't think the .exe files are different, but I will check up on that.

#### EagleEye

##### Member
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?

Last edited:

#### JohnH

##### C# Forum Moderator
Staff member
Your screenshots show you are using different versions.

#### EagleEye

##### Member
Yeah i see. That was the picture i had from one of my friends who tried it for me. I have updated the picture above.

#### JuggaloBrotha

Staff member
Could you zip and attach your project here?

#### JuggaloBrotha

Staff member
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.

#### EagleEye

##### Member
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?

#### JuggaloBrotha

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

How can i solve this?
What non-US region is that computer set to?

#### EagleEye

##### Member
It's set to Danish.

Replies
3
Views
278
Replies
2
Views
206
Replies
2
Views
246
Replies
7
Views
3K
Replies
1
Views
859