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