- MT5-The value in the variable is not suitable (how to convert it to an integer)
- What function use for convert double data type to integer data type?
- Questions from Beginners MQL4 MT4 MetaTrader 4
Assigning a double to and integer variable will do it.
Take your pick: mathfloor, mathceil, mathround. These can all be used to reduce your double value to an integer without resorting to typecasting.
Take your pick: mathfloor, mathceil, mathround. These can all be used to reduce your double value to an integer without resorting to typecasting.
Not strictly true. You cannot do bit manipulation with results of those functions. I store order info in an array of doubles, but must convert stored OrderType() back to true int to check for BUY/SELL via bit mask
Is MathFloor(1.1) != 1?
Is MathCeil(0.9) != 1?
Is MathRound(1.1) != 1?
Is MathFloor(1.1) != 1?
Is MathCeil(0.9) != 1?
Is MathRound(1.1) != 1?
It seems to me that you do not understand bit manipulation & processing, which is what I was specifically addressing. Bit processing is not the same as bool.
if((myDouble & myBitMask) == myBitMask)
will not compile but
if((myInt & myBitMask) == myBitMask)
does.
uh, bit processing. I have to say that i never used that in real live. I had to study it (time ago).
I can remember that there was a "cool" example, with bit processing it is possible to swap two values without the need of a third helper variable. Might be interesting if you want tune your code to the maximum..
As an Assembler programmer for several decades, I use bits a lot. e.g. When I enter a trade, I use a bitmask to decide which SL methods to use i.e. set to 1+2+8 to use SL methods 1, 2 & 4. The actual code I use is
ThisSLMethodSeq = 1; int bitMask = 1; for(int bitTest = StopLossMethodsFlags; bitTest != 0; ThisSLMethodSeq++, bitMask = bitMask << 1) { if((bitTest & bitMask) == bitMask) { OpenSpecificShortTrade(ThisSLMethodSeq, pOptionalStopLoss); } int notMask = 0x7fffffff - bitMask; bitTest = bitTest & notMask; }
Oh, and that swap method you mentioned (very handy when RAM storage was $1 a byte) ...
int ia, ib; ia = 15; ib = 24; ia = ia ^ ib; ib = ia ^ ib; ia = ia ^ ib; if(ia != 24 || ib != 15) Alert("ia=", ia, "ib=", ib, " wrong");
but you can't use it on doubles ;-)
I have used it for swapping strings in the dim & distant past (okay, over 30 years ago), but not in MQL4.
Take your pick: mathfloor, mathceil, mathround. These can all be used to reduce your double value to an integer without resorting to typecasting.
if((MathCeil(myDouble) & myBitMask) == myBitMask)will not compile. They return the integer equivalent which is usually, but not always fine.
double minLot = MarketInfo(Symbol(), MODE_MINLOT), lotStep = MarketInfo(Symbol(), MODE_LOTSTEP); size = MathFloor(size/lotStep)*lotStep; if (size < minLot){ EA.status = status; return(0); }
Either use int v=d; or use
int Int(double d){ return(d); }
Fortunately it is easily testable. Run the attached script, investigate the math. Works for me. MQL seems to deftly manage distinguishing between true integers and "equivalent" integers as you put it and mathfloor, mathceil, and mathround all perform as expected.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use