Hi folks,
Today we are going to talk about a very important set of MQL4 functions; the Conversion functions.
In many of situations you have a variable in a specific format and you want to use it in another format.
For example: When we use the CurTime() function which returns the current server time; it returns this time in datetime format.
It will not problem for us if we are going to use this time as a datatime format for example adding to 2 hours to it or substracting it from the local time.
But if we want to display this time to the user using the Alert() function we will face a problem. the Alert() can't convert automatically from datetime data type to string data type which we want the user to see. In this case we have to use the conversion function TimeToStr().
Beside the conversions functions we are going to study I have to mention that MQL4 will make implicit conversions from a data type to other data type when you assign a wrong value for this data type.
For example:
string var1 = 100;
Alert(var1);
In the above code you have assigned an integer to a string variable. MQL4 will convert the number 100 from integer to string.
If you don't sure of that add this line:
Alert(var1+10);
What do you think you'll get? No, not 110 but you will get 10010 (Figure 1). That's because MQL4 has converted the 100 to string and 10 to string then added them to each others as strings.
Figure 1
Let's study the conversion function available in MQL4!
Syntax:
| string CharToStr( int char_code) |
Description:
The CharToStr function converts from Char type to string type; it converts the ASCII char code passed to it to string.
Parameters:
This function takes only one parameter:
int char_code
The ASCII char code of the character you want to convert it to string.
Example:
| for (int cnt = 1 ; cnt < 255 ; cnt++) |
Syntax:
| string DoubleToStr( double value, int digits) |
Description:
The DoubleToStr function converts from double data type to string type; it converts the double value passed to the function to string with the number of digits passed to the function.
Parameters:
This function takes two parameters:
double value
The double value you want to convert it to string.
int digits
The number of digits you want the function to use in converting the double value to string. it can be ranged from 0 (no digits) to 8 (8 digits).
Example:
| string value=DoubleToStr(1.2345678, 4); |
Syntax:
| double NormalizeDouble( double value, int digits) |
Description:
The NormalizeDouble function rounds the double value passed to it to the number of digits passed. It's like DoubleToStr function but it returns double value instead of string.
Parameters:
This function takes two parameters:
double value
The double value you want to round it.
int digits
The number of digits you want the function to use in rounding the double value. it can be ranged from 0 (no digits) to 8 (8 digits).
Example:
| double value=1.2345678; |
Syntax:
| double StrToDouble( string value) |
Description:
The StrToDouble function converts from string data type to double type; it converts the string value passed to the function to a double.
Parameters:
This function takes only one parameter:
string value
The string value you want convert it to double value.
Example:
| double value=StrToDouble("1.2345678"); |
Syntax:
| int StrToInteger( string value) |
Description:
The StrToInteger function converts from string data type to integer type; it converts the string value passed to the function to an integer.
Parameters:
This function takes only one parameter:
string value
The string value you want convert it to integer value.
Example:
| double value=StrToInteger("1999"); |
Syntax:
| datetime StrToTime( string value) |
Description:
The StrToTime function converts from string data type to datetime data type; the string value passed to the function must be in the format: "yyyy.mm.dd hh:mi"
Parameters:
This function takes only one parameter:
string value
The string value you want convert it to datatime data type. This string have in one of these formats:
"yyyy.mm.dd hh:mi"
"hh:mi"
"yyyy.mm.dd"
Example:
| datetime var1; |
Syntax:
| string TimeToStr( datetime value, int mode=TIME_DATE|TIME_MINUTES) |
Description:
The TimeToStr function converts from datetime data type to string data type; the return string value will be in the format "yyyy.mm.dd hh:mi".
Parameters:
This function takes two parameters:
datetime value
The datatime value you want to convert it to string. It starts from 00:00 January 1, 1970.
int mode
Optional parameter determine the mode of conversion; what the string format the function will return.
It can be one or combination of these modes:
TIME_DATE the result will be in the format "yyyy.mm.dd",
TIME_MINUTES the result will be in the format"hh:mi",
TIME_SECONDS the result will be in the format "hh:mi:ss".
The default value is TIME_DATE|TIME_MINUTES which means the result will be in the format "yyyy.mm.dd hh:mi".
Example:
| strign var1=TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS); |