In MetaTrader 4 (MT4) MQL4 programming, converting strings to various data types (Integer, Double, DateTime) makes it easier to process input data. This article explains these conversion methods in a way that is easy for beginners to understand.
What is Converting Strings to Data Types?
Converting strings to data types means transforming numbers or dates expressed as strings into corresponding data types. This allows calculations and data manipulation within the program.
Converting to Integer Type
To convert string data to Integer type, use the StrToInteger()
function. This function converts a string representing an integer value to an Integer type.
Function Specification
int StrToInteger(string value)
value
: String representing an integer value
Sample Code
string str_value = "1024";
int int_value = StrToInteger(str_value);
Print("Integer value: " + int_value);
Converting to Double Type
To convert string data to Double type, use the StrToDouble()
function. This function converts a string representing a real number to a Double type.
Function Specification
double StrToDouble(string value)
value
: String representing a real number
Sample Code
string str_value = "103.2812";
double double_value = StrToDouble(str_value);
Print("Double value: " + double_value);
Converting to DateTime Type
To convert string data to DateTime type, use the StrToTime()
function. This function converts a string representing a date and time to a DateTime type. The format needs to be “yyyy.mm.dd hh”.
Function Specification
datetime StrToTime(string value)
value
: String representing date and time
Sample Code
string str_date = "2024.07.07 12:30";
datetime datetime_value = StrToTime(str_date);
Print("DateTime value: " + TimeToStr(datetime_value));
Execution Results
Executing the sample code above yields the following results:
Integer value: 1024
Double value: 103.2812
DateTime value: 2024.07.07 12:30
Summary
By converting strings to various data types, processing data becomes simpler. In MQL4 programming, you can convert string data to Integer, Double, and DateTime types using the StrToInteger()
, StrToDouble()
, and StrToTime()
functions. Understanding these basic uses will make processing input data in your program easier. Beginners should first learn how to use these functions and then apply them to actual trading programs.