fixed format output in MQL4

 

hi,

is it possible to produce fixed format text output in MQL4? something like sprintf()? i know that DoubleToStr() can be used to specify the number of places after the decimal comma, but if you are wanting to format data into a table something like this:

1.253 103.25

92.103 92.91

105.293 1.37

then you need to be able to specify the width of the non-fractional part of the field. i know that this is pretty anal, but i would like my tables to look neat. it just makes them a whole lot easier to read.

thanks for your help!

best regards, andrew.

 

I am not sure whether this is what you mean but you can create a string that has many pieces of data on it by using StringConcatenate. The string can be printed or shown as a comment, etc. See below:

string MktInfo = StringConcatenate(1.253, " ", 103.25);

That is what I presume you meant when I look at the code box you made.

However, when people talk about tables they usually mean arrays. You can create arrays in MQL. I am sure there are plenty of articles on this site if you search for arrays.

Chris

 

I've just recently started working with File_Read and File_Write. What I assume you're looking for is creating a Excel looking text file. Personally haven't seen any commands which lets you do this. Mql4 assumes we all have a Spread_Sheet program installed on our computers. Therefore, the preferred format I'm used to seeing is the .CSV files. Those are Comma separated text files. They look nice in columns and rows when opened in a spreadsheet program. You can also use Semi-colon separated files.

 

If you insist on using text files, than you'll need to get a little more creative. Using allot of blank space "  " like the example above. You'll also need good placement of the line_break tokens. Well that's my 2-cents anyway until someone else who have a better alternative comes along.

 

okay, so i got off my ass and wrote it myself.

string FormatNumber(double x, int width, int precision)
{
   string p = DoubleToStr(x, precision);
   
   while(StringLen(p) < width) p = " "+p;
   
   return(p);
}

the typical usage is something like:

Print(FormatNumber(x, 9, 3));
Reason: