NavigationUser loginWho's new
Who's onlineThere are currently 0 users and 68 guests online.
Recent blog posts
|
Conversion functionsHi folks, Today we are going to talk about a very important set of MQL4 functions; the Conversion functions. Why should I use 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. 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().
Implicit conversions: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; In the above code you have assigned an integer to a string variable. MQL4 will convert the number 100 from integer to string. 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!
CharToStr:Syntax:
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:
DoubleToStr:Syntax:
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:
NormalizeDouble:Syntax:
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:
StrToDouble:Syntax:
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:
StrToInteger:Syntax:
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:
StrToTime:Syntax:
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: Example:
TimeToStr:Syntax:
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. The default value is TIME_DATE|TIME_MINUTES which means the result will be in the format "yyyy.mm.dd hh:mi". Example:
|