How di I change odd numbers to even (part 2)?

 

The original problem is how to determine if a 2-decimal double number (such as .67 or 2.51) has an even or odd last digit and thereby

make it even by subtracting .01 (which is the broker's lot step amount).

One recommendation was to use MathMod and/or NormalizeDouble. I don't see how those are effective.

Here are some coded variations using those two functions.

double x1 = 2.51;
double y1 = 2;
double myRemainder1 = MathMod(x1, y1);
Alert ("myRemainder1 from MathMod(2.51, 2) = ", myRemainder1); // myRemainder1 = 0.51

double x2 = 2.51;
double y2 = 2;
double myRemainder2 = NormalizeDouble(MathMod(x2, y2), 2);
Alert ("myRemainder2 from NormalizeDouble(MathMod 2.51, 2) = ", myRemainder2); // myRemainder2 = 0.51

double x3 = .67;
double y3 = 2;
double myRemainder3 = MathMod(x3, y3);
Alert ("myRemainder3 from MathMod(.67, 2) = ", myRemainder3); // myRemainder3 = 1

double x4 = .67;
double y4 = 2;
double myRemainder4 = MathMod(x4, y4);
Alert ("myRemainder4 from NormalizeDouble(MathMod(.67, 2) = ", myRemainder4); // myRemainder4 = 1

As you can see, in each case the results are not usable.

If there is no solution using a math technique, then I will deconstruct the number to a string, change the last digit, and reconstruct the number.

 

Provided that a double variable 'lots' is already normalized to two digits (e.g. using NormalizeDouble()):


lots = lots - MathMod( lots, 2*MarketInfo( Symbol(), MODE_LOTSTEP ) );
 
chaffinsjc wrote >>

The original problem is how to determine if a 2-decimal double number (such as .67 or 2.51) has an even or odd last digit and thereby

make it even by subtracting .01 (which is the broker's lot step amount).

One recommendation was to use MathMod and/or NormalizeDouble. I don't see how those are effective.

Here are some coded variations using those two functions.

double x1 = 2.51;
double y1 = 2;
double myRemainder1 = MathMod(x1, y1);
Alert ("myRemainder1 from MathMod(2.51, 2) = ", myRemainder1); // myRemainder1 = 0.51

double x2 = 2.51;
double y2 = 2;
double myRemainder2 = NormalizeDouble(MathMod(x2, y2), 2);
Alert ("myRemainder2 from NormalizeDouble(MathMod 2.51, 2) = ", myRemainder2); // myRemainder2 = 0.51

double x3 = .67;
double y3 = 2;
double myRemainder3 = MathMod(x3, y3);
Alert ("myRemainder3 from MathMod(.67, 2) = ", myRemainder3); // myRemainder3 = 1

double x4 = .67;
double y4 = 2;
double myRemainder4 = MathMod(x4, y4);
Alert ("myRemainder4 from NormalizeDouble(MathMod(.67, 2) = ", myRemainder4); // myRemainder4 = 1

As you can see, in each case the results are not usable.

If there is no solution using a math technique, then I will deconstruct the number to a string, change the last digit, and reconstruct the number.

Well, gosh, that works. Thank you very much. (Now I'm gonna spend awhile figuring out why). Again, thank you.

Reason: