Discussing the article: "Studying PrintFormat() and applying ready-made examples"

 

Check out the new article: Studying PrintFormat() and applying ready-made examples.

The article will be useful for both beginners and experienced developers. We will look at the PrintFormat() function, analyze examples of string formatting and write templates for displaying various information in the terminal log.

Displaying values in the log or on the monitor screen is a simple and familiar operation until you need to show something more complex than "Hello, world". But sooner or later a situation arises when you need to make a formatted output of a value or property that is not very often in demand. Of course, you can look into the MQL5 help.


But sometimes you want to have a ready-made collection of recipes for displaying all kinds of information provided by the MetaTrader 5 terminal. In this article, we will try to understand the intricacies of calling the PrintFormat function and write ready-made templates you can simply insert into your code.

Author: Artyom Trishkin

 

Thank you. Very informative, but not from the first reading...))))))

Reading the help when writing code is somehow painful and time-consuming. That's why I don't understand much... I hope this article will help me to understand the subtleties of PrintFormat() and StringFormat() better.

 
Perhaps the only non-obvious point in the help is not covered - in what cases it can be really useful to specify the data size specifier (h | l | ll | I32 | I64) if everything works without it.
 
JRandomTrader #:
Perhaps the only non-obvious point not covered in the help is when specifying the data size specifier (h | l | ll | I32 | I64) can be really useful if everything works without it.

Look at this example

//+------------------------------------------------------------------+
//| Script programme start function|
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   long x = (long)0xFFFFFFFF * 1000;

   Print("\nDEC: ",  typename(x)," x=",x,"\n---------");
   Print("%hu: ",  StringFormat("%hu",x));
   Print("%u: ",   StringFormat("%u",x));
   Print("%I64u: ",StringFormat("%I64u",x));
   Print("%llu: ", StringFormat("%llu",x));
   Print("\nHEX: ",  typename(x)," x=",StringFormat("%llx",x),"\n---------");
   Print("%hx: ",  StringFormat("%hx",x));
   Print("%x: ",   StringFormat("%x",x));
   Print("%I64x: ",StringFormat("%I64x",x));
   Print("%llx: ", StringFormat("%llx",x));

  }

Result

2023.07.12 17:48:12.920 PrinFormat (EURUSD,H1)  DEC: long x=4294967295000
2023.07.12 17:48:12.920 PrinFormat (EURUSD,H1)  ---------
2023.07.12 17:48:12.920 PrinFormat (EURUSD,H1)  %hu: 64536
2023.07.12 17:48:12.920 PrinFormat (EURUSD,H1)  %u: 4294966296
2023.07.12 17:48:12.920 PrinFormat (EURUSD,H1)  %I64u: 4294967295000
2023.07.12 17:48:12.920 PrinFormat (EURUSD,H1)  %llu: 4294967295000
2023.07.12 17:48:12.920 PrinFormat (EURUSD,H1)  
2023.07.12 17:48:12.920 PrinFormat (EURUSD,H1)  HEX: long x=3 e7fffffc18
2023.07.12 17:48:12.920 PrinFormat (EURUSD,H1)  ---------
2023.07.12 17:48:12.920 PrinFormat (EURUSD,H1)  %hx: fc18
2023.07.12 17:48:12.920 PrinFormat (EURUSD,H1)  %x: fffffc18
2023.07.12 17:48:12.920 PrinFormat (EURUSD,H1)  %I64x: 3 e7fffffc18
2023.07.12 17:48:12.920 PrinFormat (EURUSD,H1)  %llx: 3 e7fffffc18
 
JRandomTrader #:
Perhaps the only non-obvious point in the help is not covered - in what cases it can be really useful to specify the data size specifier (h | l | ll | I32 | I64) if everything works without it.

Of course it works. But just a couple of hours ago I was thinking about the same question and I had a thought: if we pass only double-values to a function, and then inside, based on some criteria, we understand that these double-data should be long-data. This is where we specify their size. Just a thought out loud. I haven't checked it.

 

And how did I live all these years without printformat.... I'll probably continue to do so.

 
Dmitry Fedoseev #:

And how did I live all these years without printformat.... I'll probably continue to do so.

Probably

 

Artem, I don't understand where this came from

   uint w=(header_width==0 ? header.Length()+1 : header_width);

I didn't plug in any library. Or am I completely blind?

 
Alexey Viktorov #:

Artem, I don't understand where that came from

I didn't plug in any library. Or am I completely blind?

This is now a standard functionality for string variables and arrays.

 
Artyom Trishkin #:

This is now standard functionality to string variables, to arrays.

Thank you. But there's nothing in the documentation about it at all. What kind of number does it return. Is it a duplicate of StringLen()?

 
Alexey Viktorov #:

Thank you. But there's nothing in the documentation about it at all. What kind of number does it return. Is it a duplicate of StringLen()?

Yes