The best way to round numbers to the thousands -- not decimal places ?
- Position Size Returning Negative Value
- HedgeEA
- additional decimal points for lotsize
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
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.
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);