mql4

Rounding in mql4


Used in mql4 language NormalizeDouble() function sometimes does not work properly. You can see in the example below.

double value=0.9731300000000001;
double value2=NormalizeDouble(value,Digits);
Print(value2);

As a result, although I want to get the result of 0.97313, the result is 0.9731300000000001. How can I fix.

double Yuvarla(double Sayi) 
{ 
    double _sayi1=MathRound(Sayi*MathPow(10,Digits)); 
    double _sayi2=_sayi1/MathPow(10,Rakam); 
    return _sayi2; 
} 

this method didn't work either


Solution

  • Results such as this are normal, not only in MQL4 but also in other programming languages such as Python. It relates to the way the floating-point format is built and the approximation that is being done in representing these values.

    Practically, you should not worry using these values for your calculations, unless for some reason it is critical to have a precision level that goes beyond 10^-Digits.

    However, if your objective here is to represent them as a string and limit their representation to the amount of Digits in the current market - which is how it looks like based on your examples - then you can use Print(DoubleToString(YourValue, Digits)).