I know this is an old post, I'm just adding the function I wrote to perform bankers rounding the same way that Math.Round in C# does it in case someone stumbles across this post looking for the same info I was. The code above didn't produce the same results as Math.Round, so I wrote one of my own :-)
MQL code:
double RoundNumber(double dNumber, int nDigits)
{
// Use bankers rounding - if the numbers after nDigits = 5, Round up when the number before it is odd, round down when it's evendouble dRet = dNumber;
double dInt = (int)(dNumber * MathPow(10, nDigits));
double dRemainder = NormalizeDouble((dNumber * MathPow(10, nDigits)) - dInt, nDigits);
if (dRemainder == 0.5)
{
if (MathMod(dInt, 2) != 0) // Means the number is odd
dRet = (dInt + 1) * MathPow(10, -nDigits);
else
dRet = dInt * MathPow(10, -nDigits);
}
else
dRet = NormalizeDouble(dNumber, nDigits);
return (dRet);
}
Notice that calling Math.Round(dNumber, nDigits, MidpointRounding.AwayFromZero); will round the same way as NormalizeDouble...if you were looking to have C# code act the same way as your EA.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Thanks all for help me,
I using code of Thirteen. It can round a number 1.23455 to 1.2345 ( 4 digits) and 1.23456 to 1.2346
Cool, just for good measure using everyone's favourite function NormalizeDouble...
OR
I know this is an old post, I'm just adding the function I wrote to perform bankers rounding the same way that Math.Round in C# does it in case someone stumbles across this post looking for the same info I was. The code above didn't produce the same results as Math.Round, so I wrote one of my own :-)
MQL code:
And if you want to validate it in CSharp
Notice that calling Math.Round(dNumber, nDigits, MidpointRounding.AwayFromZero); will round the same way as NormalizeDouble...if you were looking to have C# code act the same way as your EA.
Cheers
a bit late :)
but how about