C# Denies simple math? :\
-
I've added a progress bar to my app and i'm trying to update it but the result is only 0 and then 100.
int t = int.Parse(textBox1.Text); double s = ((p/t)*100); progressBar1.Value = (int)s;
t (textBox1) is how many times to do the operation. p is the counter for how many times it has been done. ive followed the program with breakpoints and s is always 0.0, even when p is 10 and t is 100 ((10/100)*100) = 10. wtf? :\ -
I've added a progress bar to my app and i'm trying to update it but the result is only 0 and then 100.
int t = int.Parse(textBox1.Text); double s = ((p/t)*100); progressBar1.Value = (int)s;
t (textBox1) is how many times to do the operation. p is the counter for how many times it has been done. ive followed the program with breakpoints and s is always 0.0, even when p is 10 and t is 100 ((10/100)*100) = 10. wtf? :\Try this: double s = p * 100 / t; It doesn't matter what order you do divide and multiply on paper, you get the same result. However, if you divide by an int, you get an int and if it's < 1, it will round to an int, so the * 100 isn't doing what you'd hoped. double t = double.Parse(textBox1.Text); double s = ((p/t)*100); or int t = int.Parse(textBox1.Text); double s = ((p/(double)t)*100); would also work.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
I've added a progress bar to my app and i'm trying to update it but the result is only 0 and then 100.
int t = int.Parse(textBox1.Text); double s = ((p/t)*100); progressBar1.Value = (int)s;
t (textBox1) is how many times to do the operation. p is the counter for how many times it has been done. ive followed the program with breakpoints and s is always 0.0, even when p is 10 and t is 100 ((10/100)*100) = 10. wtf? :\I believe that you are a former VB programmer? In VB the / operator is only a floating point operator, and the integer division operator is \. In C# the / operator is defined for both data types, so which one is used depends on the data you use it on. The exact equivalent of p/t in VB would in C# be (double)p/(double(t), as VB would automatically convert the values to accommodate the operator.
--- single minded; short sighted; long gone;
-
I believe that you are a former VB programmer? In VB the / operator is only a floating point operator, and the integer division operator is \. In C# the / operator is defined for both data types, so which one is used depends on the data you use it on. The exact equivalent of p/t in VB would in C# be (double)p/(double(t), as VB would automatically convert the values to accommodate the operator.
--- single minded; short sighted; long gone;
Is there any reason to cast p to double ? I thought casting t to double is all that's needed here.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Is there any reason to cast p to double ? I thought casting t to double is all that's needed here.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Yes, casting only one of the operands is the minimum required to make it work. That would decide what operator to use, so the other operator will also be cast to the same type. Whether it's done implicitly or explicitly, the generated code will be the same. I prefer doing explicit casting in situations like this. It shows the intention of the code more clearly than if one relies on implicit conversions.
--- single minded; short sighted; long gone;