The best way to round numbers to the thousands -- not decimal places ?

 
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.
 
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
 

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:

Excellent! Both of these work perfectly. I could have fiddled with this all afternoon and still not figured it out.

 

Yep,  there is usually more than one way to do most things in mql4  :-)
 
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.
 

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
 
I see - this is important. Yes, the truncation is what I want.
 

You can also truncate using target typecasting:

double x = 21345.6789; int y = 0;
y = x / 1000;
y *= 1000;
Print (y);
 
MisterDog:
I see - this is important. Yes, the truncation is what I want.

Just removed the initial addition of 500 in the solution I suggested . . .

double tradeUnits = 21345.6789;
  int newTradeUnits = MathFloor(tradeUnits / 1000) * 1000;
  Print(newTradeUnits);
Reason: