Forex EA Programming Tutorial - Data Type Conversion
The MQL language currently only supports implicit conversions, and MQL can currently do automatic data type conversions.
Int (bool, color, datetime) can be converted to double or string
Double can be converted to string. In addition to the above two conversions, other data type conversions cannot be performed, such as:
Int i = 1/2;//No data conversion is performed, note that the result is 0, not 0.5, because both the numerator and denominator are integers, so no conversion is performed.
Int i = 1/2.0;//The expression has been converted to double type, and the calculated result is 0.5, but since the variable i is defined as int type, the whole part of 0 is taken, and the final result is still integer 0.
Double d = 1.0/1.0;//without any conversion, the result is 0.5.
Double d = 1/2.0;//Since the denominator is a double, the numerator is converted to a double, which is 1.0, so the result of this expression is 0.5.
Double d = 1/2;//Since both the numerator and denominator are integers, the calculated result is integer 0, and the target dstring s = NULL;//The result is "0", note that the value of NULL is 0
String's = "abc" + 123;//The result is "abc123", which was originally an integer and was converted to string type.
Note that the string type cannot be implicitly converted to int and double,CXM ForexTherefore, the following expressions are all incorrect.
int i = "abc";
The type is double, so the final result is converted from integer 0 to double 0.0.
String's = 1.0/8;//Since there is a double in the numerator and denominator, the non-double in the numerator and denominator will be converted to double type, and the result of the expression is 0.12500000. Since the target type is string, all final results are string type, and the value is "0.12500000". Note that there are double quotes before and after the value.
If the string "123" is to be converted to the integer 123, it needs to be converted using the function, which will be mentioned in a later lesson.
Please indicate the source: https://www.daoctech.com/fxoutrade/2681.html
Disclaimer: The content of this article does not represent the views of the Institute of Foreign Exchange. The content is for reference only and does not constitute investment advice. Investors operate accordingly at their own risk.
