I'm trying to figure out the best way to change something like this, 21345.6789 to this, 21000. NormalizeDouble seems to work in decimal places but I would like to round down to the nearest 1000s. I'm trying to use DoubleToStr but it seems like there's got to be a better way.
- Teach how to hash data in mql4
- HedgeEA
- Alert: MT4 Terminal Duplicating Orders Sent by Experts
MisterDog:
I'm trying to figure out the best way to change something like this, 21345.6789 to this, 21000. NormalizeDouble seems to work in decimal places but I would like to round down to the nearest 1000s. I'm trying to use DoubleToStr but it seems like there's got to be a better way.
Add 500, divide by a 1000, use this value with MathFloor() and finally multiply by 1000
I'm trying to figure out the best way to change something like this, 21345.6789 to this, 21000. NormalizeDouble seems to work in decimal places but I would like to round down to the nearest 1000s. I'm trying to use DoubleToStr but it seems like there's got to be a better way.
What about something like this:
double x = 21345.6789; x = MathRound(x / 1000) * 1000; Print (x);
Excellent! Both of these work perfectly. I could have fiddled with this all afternoon and still not figured it out.
double tradeUnits = 21345.6789; int newTradeUnits = MathFloor((tradeUnits + 500) / 1000) * 1000; Print(newTradeUnits); // Print output: 21000 double x = 21345.6789; x = MathRound(x / 1000) * 1000; Print (x); // Print output: 21000
MisterDog:
Yep, there is usually more than one way to do most things in mql4 :-)
Excellent! Both of these work perfectly. I could have fiddled with this all afternoon and still not figured it out.
Hold on! MathFloor rounds down and MathRound rounds up. I need to round down so I am using the MathFloor solution.
MisterDog:
Hold on! MathFloor rounds down and MathRound rounds up. I need to round down so I am using the MathFloor solution.
You said "The best way to round numbers to the thousands" the convention for rounding is that 0.5 goes to 1 less than 0.5 goes to 0, so in your case 1500 would go to 2000 and 1499 to 1000 you should find that both solutions offered do this. If you need something different then you will need to modify the code.
Hold on! MathFloor rounds down and MathRound rounds up. I need to round down so I am using the MathFloor solution.
You said rounding. There is no round down/round up, there is only rounding.
If you want to truncate to the lower 1000 don't add the (x+500)/1000 or equivalent x/1000+0.5
// MathFloor((tradeUnits + 500) / 1000) * 1000; // 1500 -> 2000 MathFloor(tradeUnits / 1000) * 1000; // 1999 -> 1000
You can also truncate using target typecasting:
double x = 21345.6789; int y = 0; y = x / 1000; y *= 1000; Print (y);
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register