I’m doing simple divisions in c#, and I am a bit puzzled by its intricacies. Here’s some code, and in the comments, the result. (btw, I only compile with 1 line not commented, if you say that I have 5 declarations of the same variable)
double result = 2 / 3; //gives 0
double result = Convert.ToDouble(2) / Convert.ToDouble(3); // is good
double result = double.Parse(2) / double.Parse(3); // gives me errors
double result = double.Parse(2 / 3); // gives me errors
double result = Convert.ToDouble(2 / 3); // gives 0
MessageBox.Show(result.ToString());
so if you have a bunch of integers you wanna mess with, you have to convert each one to a double. pretty tedious…
Integer division discards the remainder. You don’t need to use
Convert.ToDoubleordouble.Parse, you can simply write:or
If either one of the operands is a floating-point value then you get floating-point arithmetic instead of integer arithmetic.
To explain each one:
Hope that helps explain it.