MQL4|DataType To String Conversion Guide

※記事内に広告を含む場合があります。

In MetaTrader 4 (MT4) MQL4 programming, converting various data types to strings makes it easier to display data and output logs. This article explains how to convert Char, Double, and DateTime data types to strings in a way that’s easy for beginners to understand.

What is String Conversion?

String conversion refers to transforming various data types (Char, Double, DateTime) into human-readable string formats. This simplifies data display and log output within the program.

Char Type String Conversion

To convert Char type data to a string, use the CharToStr() function. This function converts ASCII character codes to their corresponding string.

Function Specification

string CharToStr(int char_code)
  • char_code: ASCII character code

Sample Code

int char_code = 68; // ASCII code for 'D'
string char_str = CharToStr(char_code);
Print("String: " + char_str);

Double Type String Conversion

To convert Double type data to a string, use the DoubleToStr() function. This function converts a Double type value to a string with a specified number of decimal places.

Function Specification

string DoubleToStr(double value, int digits)
  • value: Value to convert
  • digits: Number of decimal places (0–8)

Sample Code

double value = 1.23456789;
string double_str = DoubleToStr(value, 5);
Print("String: " + double_str);

DateTime Type String Conversion

To convert DateTime type data to a string, use the TimeToStr() function. This function converts DateTime type data to a string in a specified format.

Function Specification

string TimeToStr(datetime value, int mode = TIME_DATE | TIME_MINUTES)
  • value: DateTime data
  • mode: Format (TIME_DATE, TIME_MINUTES, TIME_SECONDS)

Sample Code

datetime current_time = TimeCurrent();
string time_str = TimeToStr(current_time, TIME_DATE | TIME_SECONDS);
Print("Current DateTime: " + time_str);

Execution Result

Executing the sample code above yields the following results:

String: D
String: 1.23457
Current DateTime: 2024.07.07 12:30:45

Summary

Converting various data types to strings simplifies data display and log output. In MQL4 programming, you can convert Char, Double, and DateTime data types to strings using the CharToStr(), DoubleToStr(), and TimeToStr() functions, respectively. Understanding the basic usage of these functions makes debugging and data display easier. Beginners should start by learning how to use these functions and apply them to actual trading programs.

※記事内に広告を含む場合があります。
ベアちゃん@東京シストレ: One of Japan's oldest MetaTrader FX automated trading developers 🔧. With over 15 years of professional experience, he boasts achievements such as winning first place in the Tradency Tournament '15 🥇 and being the runner-up in the 3rd EA-1 Grand Prix 🥈. He has also appeared on Radio Nikkei and is currently active as an executive of Trilogy Corporation (a registered investment advisory and agency business) [Registration number: 372, Director of the Kinki Finance Bureau, Ministry of Finance]. To join the board of Trilogy Corporation, he underwent a rigorous personal examination by the Kinki Finance Bureau, and his name is now registered with the Kinki Finance Bureau. Becoming an executive officer of a registered investment advisory and agency business requires passing this stringent personal examination. Utilizing his extensive knowledge as a former senior design engineer, he provides insights into the development of high-performance Expert Advisors (EAs), the latest trading technologies, and market analysis. He delivers professional strategies and tips to traders aiming to optimize their FX trading and maximize profits.

This website uses cookies.