why return many decimal ?? also after normalize double? - page 2

 
Marcin Madrzak #:
StringFormat

for me not work also stringformat where is the problem f.....

//+------------------------------------------------------------------+
//|                                                da_cancellare.mq4 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

/*

#import "testdll.dll"
    int Somma(int a, int b);
#import



int OnInit()
{
int risultato = Somma(5, 7);
Print("La somma di 5 e 7 è: ", risultato);


   return(INIT_SUCCEEDED);
}

*/


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
  
  
  
  
//--- indicator buffers mapping
       double valore1 = 1.1234500000;
    double valore2 = iOpen("AUDJPY", 0, 24);
    
    double valore3=FormatDoubleToCustomString(valore2);
          Print ("FirstPriceVCFirstPriceVCFirstPriceVCFirstPriceVCFirstPriceVC---> "+valore3);

//---

   return(INIT_SUCCEEDED);
  }
  

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
  
  
 
string FormatDoubleToCustomString(double number)
{
    string formattedString = StringFormat("%.8lf", number); // Formatta il numero con otto cifre totali
    int dotIndex = StringFind(formattedString, '.'); // Trova l'indice del punto decimale
    
    if (dotIndex != -1)
    {
        // Rimuovi gli zeri inutili dalla parte decimale
        while (StringSubstr(formattedString, StringLen(formattedString) - 1, 1) == "0")
        {
            formattedString = StringSubstr(formattedString, 0, StringLen(formattedString) - 1);
        }
        
        // Rimuovi il punto decimale se non ci sono cifre dopo di esso
        if (StringSubstr(formattedString, StringLen(formattedString) - 1, 1) == ".")
        {
            formattedString = StringSubstr(formattedString, 0, StringLen(formattedString) - 1);
        }
    }
    
    return formattedString;
}
 

%.xf where x is your number of digits after decimal point, e.g. %.3f gives you 123.456

#property strict
void OnStart()
  {
   double number=Open[0];
   
   Print(DoubleToStr(number,_Digits));
   
   //--- OR
   
   string format = StringFormat("%%.%df", _Digits); // generates a format string with %.xf where x is number of digits after the decimal point
   PrintFormat(format, number);
  }
 
Marcin Madrzak #:

%.xf where x is your number of digits after decimal point, e.g. %.3f gives you 123.456

thanks now  work

 

I do not have MT4 , but you could do this test to check

"print_check.mq4" script:

void OnStart()
{
    Print(0.01);
    Print(0.02);
    Print(0.03);
    Print(0.04);
    Print(0.05);
    Print(0.06);
    Print(0.07);
    Print(0.08);
    Print(0.09);

    Print(0.1);
    Print(0.2);
    Print(0.3);
    Print(0.4);
    Print(0.5);
    Print(0.6);
    Print(0.7);
    Print(0.8);
    Print(0.9);
}

If the output in experts log is something different than:

 0.01
 0.02
 0.03
 0.04
 0.05
 0.06
 0.07
 0.08
 0.09
 0.1
 0.2
 0.3
 0.4
 0.5
 0.6
 0.7
 0.8
 0.9

Then, the issue with the Print() function in MetaTrader 4 is not fixed  (as MT4 is no longer maintained).

 
amrali #:

I do not have MT4 , but you could do this test to check

"print_check.mq4" script:

If the output in experts log is something different than:

Then, the issue with the Print() function in MetaTrader 4 is not fixed  (as MT4 is no longer maintained).

quoting  the problem i must use  "   not '  in int dotIndex = StringFind(numStr, ".");  thanks  again

 

You could try this function from 'math_utils' library https://www.mql5.com/en/code/25723

string toString(const double value)
  {
   string str = NULL;

   for(int sig = 15; sig <= 17; sig++)
      if(value == StringToDouble(str = StringFormat("%." + (string)sig + "g", value)))
         break;

   return str;
  }

Now, re-try the previous example:

void OnStart()
{
    Print( toString(0.01) );
    Print( toString(0.02) );
    Print( toString(0.03) );
    Print( toString(0.04) );
    Print( toString(0.05) );
    Print( toString(0.06) );
    Print( toString(0.07) );
    Print( toString(0.08) );
    Print( toString(0.09) );

    Print( toString(0.1) );
    Print( toString(0.2) );
    Print( toString(0.3) );
    Print( toString(0.4) );
    Print( toString(0.5) );
    Print( toString(0.6) );
    Print( toString(0.7) );
    Print( toString(0.8) );
    Print( toString(0.9) );
}

Also there is another useful function in the library:

// Formats double with thousands separator and specified decimals.
string FormatDouble(const double number, const int digits, const string separator=",");
Math Utils (MT4)
Math Utils (MT4)
  • www.mql5.com
Handy functions for comparison, rounding, formatting and debugging of doubles (prices, lots and money).
Reason: