Loading…

String Formatting Helper

Both MQL4 and MQL5 give you DoubleToString and IntegerToString for turning numbers into text, and their own built-in StringFormat for printf-style interpolation. Neither one pads a column to a fixed width, groups a balance into thousands, or turns a tick count in seconds into "1h 23m 45s". Every EA that prints a status table ends up hand-rolling these, usually inconsistently across files. This header is intentionally narrow. It does not duplicate general string utilities such as trimming or case conversion - see StringUtils elsewhere in this catalogue for those. Everything here exists to make a printed line line up: StrPadLeft, StrPadRight and StrPadCenter never truncate, because a formatting helper that silently drops data to fit a width is worse than a column that runs long. FormatNumber groups the integer part with commas while leaving the sign and decimal point outside the grouping logic entirely, which is what keeps the grouping code simple enough to trust. FormatBytes and FormatDuration convert raw counters - file sizes, elapsed seconds - into the units a person actually reads. None of the functions here is named StringFormat. That name is already taken by the platform's own built-in on both MQL4 and MQL5, and shadowing it would either fail to compile or silently hide the built-in for the rest of the file - a trap worth naming explicitly rather than discovering by accident. Notes and limitations - Padding and repeat functions build the result with string concatenation in a loop. That is fine for the widths and counts a display line needs; it is not intended for building large strings in a hot tick loop. True on both platforms. - FormatNumber trusts DoubleToString for the underlying digit conversion, so it inherits the same rounding behaviour (round-half-away-from-zero) on both platforms. - FormatDuration treats the input as elapsed seconds, not a calendar duration - it does not account for months having different lengths. - GroupThousandsDigits only accepts a string of plain ASCII digits; anything else, including an already-negative or decimal string, is returned unchanged rather than guessed at.

Script · MT5 · v1.0 · Free

About String Formatting Helper

String Formatting Helper is a script for MetaTrader 5. It has 1 setting, such as Column width. MQL source code included.

Files in the download: StringFormat.mq5.mqh, StringFormat.txt, StringFormatDemo.ex5, StringFormatDemo.mq5.

Settings

SettingDefaultWhat it does
Column width12Width used for the padding demonstration

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria