How is this possible: 5/43 = 0 ???

 

I have a function:

Alert("Result: ", DoubleToStr(MyFunction(5),2));

double MyFunction(int i) {
   int k = 43;
   return(i/k);
}

// Result should be: 0.11
// Result returned is: 0.00

I've tried it with and without the 'DoubleToStr()' function! The returned result is always the same: 0.00!

I've also tried:

   return(NormalizeDouble(i/k,5));

... but still the same problem!


What am I missing here???

Thank you for your time!!



Sincerely Yours,

nanquan

 

Typecasting. And all your variables are Integers. The (i) and the (k).

Only implicit type casting is used in MQL 4 expressions. The type priority grows at casting:
int  (bool,color,datetime);
double;
string;

Before operations (except for the assignment ones) are performed, the data have been conversed into the maximum priority type. Before assignment operations are performed, the data have been cast into the target type.

Examples:
int    i = 1 / 2;     // no types casting, the result is 0
int    i = 1 / 2.0;   // the expression is cast to the double type, then is to the target type of int, the result is 0
double d = 1.0 / 2.0; // no types casting, the result is 0.5
double d = 1 / 2.0;   // the expression is cast to the double type that is the same as the target type, the result is 0.5
double d = 1 / 2;     // the expression of the int type is cast to the target type of double, the result is 0.0
string s = 1.0 / 8;   // the expression is cast to the double type, then is to the target type of string, the result is "0.12500000" (the string containing 10 characters)
string s = NULL;      // the constant of type int is cast to the target type of string, the result is "0" (the string containing 1 character)
string s = "Ticket #"+12345; // the expression is cast to the string type that is the same as the target type, the result is "Ticket #12345"

Type casting is applied to not only constants, but also variables of corresponding types.
 
Thank you ubzen!! I was sure int/int would produce double type result!! -_-
Reason: