Truncation of numeric values for display of label rounded to two decimal places that is no longer seeming to function correctly/consistently

 

Hi guys,

I have the following code that I use in many of my MQL4-based robots and am in the process of migrating them all to MQL5.

For some reason, occassionally the formatted label will display, and continue to display the value "3" for some time, then corrects itself and continues on like the "3" never existed.

This code previously worked beautifully in MQL4, though doesn't seem quite so reliable in MQL5, possibly due to the code now getting an "implicit conversion from 'number' to 'string'" message, which I can only assume is the likely cause of the issue.

It's most definitely not the logic of the robot itself causing the appearance of the extraneous number as it occurs in numerous labels sporadically, and those label calculations come from different sections of code, with the only common code that I can narrow it down to be being the TruncateNumber function.

//+------------------------------------------------------------------+
//| Truncate number to two decimal places function                   |
//+------------------------------------------------------------------+
string TruncateNumber(string number, int decimalPoints=2)
{
   int start_index = StringFind(number, ".");
   if (start_index == -1) return number;
   
   string vals[2] = {"", ""};
   StringSplit(number, '.', vals);
   
   if (StringLen(vals[1]) <= decimalPoints ) return number;
   
   return StringConcatenate(vals[0], ".", StringSubstr(vals[1], 0, 2));
}
//+------------------------------------------------------------------+

I've always been terrible with string manipulation and regex's, so any ideas with this would be greatly appreciated.

I'm using version 5.00 build 2392 (11 Oct 2021) if that might help also.

Regards,


Christian


 

Why don't you convert the number 'double' to type 'string' using DoubleToString ?

Example:

//+------------------------------------------------------------------+
//|                                                     Script 1.mq5 |
//|                              Copyright © 2021, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2021, Vladimir Karputov"
#property version   "1.000"
#property script_show_inputs
//--- input parameters
input double InpDouble  = 12.36547;
//---
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   Print(DoubleToString(InpDouble,2));
//---
  }
//+------------------------------------------------------------------+

Result:

2021.12.08 05:48:51.263 Script 1 (EURJPY,H1)    12.37
Documentation on MQL5: Conversion Functions / DoubleToString
Documentation on MQL5: Conversion Functions / DoubleToString
  • www.mql5.com
DoubleToString - Conversion Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
Script_1.mq5  2 kb
 
Vladimir Karputov #:

Why don't you convert the number 'double' to type 'string' using DoubleToString ?

Hi Vladimir,

I just did exactly that in fact, so looks like I no longer need that code. Thanks for the suggestion.

Regards,

Christian

 

Check also this link:

mql5.com/en/code/20822

Math Utils
Math Utils
  • www.mql5.com
Handy functions for comparison, rounding, formatting and debugging of doubles (prices, lots and money).