Rounding up price values mql5

 

How do toy round up or down price value to whole numbers in Mql5? for example:


EUR/USD = price now: 1.1796;

Round up: = 1.1800

round down:=1.1700

USD/JPY: Price now= 109.45

Round up: = 110:00

round down:=109:00

Thanks.

 
Math Utils
Math Utils
  • www.mql5.com
Handy functions for comparison, rounding, formatting and debugging of doubles (prices, lots and money).
 

The link does not tell you how to do that, if i use

double Floorx= MathFloor(bid);

for GBP USD 1.38671 I get 1, but i want to tell the function to return 1.3800. How do I modify this to work for all symbols?

 
Build a MathRound Function that takes a parameter for the digits to consider. Then you can control the rounding process.

Other than that, use MathRound (>your value< * MathPow(10, >your digits<)) / MathPow(10, >your digits<)


 
Dominik Egert:
Build a MathRound Function that takes a parameter for the digits to consider. Then you can control the rounding process.

Other than that, use MathRound (>your value< * MathPow(10, >your digits<)) / MathPow(10, >your digits<)

Can you give example code?
 
Can't you read? I did.
 
#include <math_utils.mqh>
void OnStart()
  {
   double price = 1.1796;
   PrintFormat("price = %.4f, Round down = %.4f, Round up = %.4f",
               price, RoundToDigitsDown(price, 2), RoundToDigitsUp(price, 2));
  }

// Output: 
// price = 1.1796, Round down = 1.1700, Round up = 1.1800

 
Peter Kaiza: How do toy round up or down price value to whole numbers in Mql5? for example:


EUR/USD = price now: 1.1796;

Round up: = 1.1800

round down:=1.1700

MT4:NormalizeDouble - General - MQL5 programming forum #3.3 (2017.01.04)
          How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum #2 (2017.05.19)
 
How do you adjust this to cater for instruments like audusd  0.7369 to 0.7400? Or usdjpy 109.59 to 110.00? The former you want to round up decimals while the latter you round up integer?
 
Peter Kaiza:
How do you adjust this to cater for instruments like audusd  0.7369 to 0.7400? Or usdjpy 109.59 to 110.00? The former you want to round up decimals while the latter you round up integer?

You seem that you have missed your 'rounding class' in school days.

 

Flawed!

#define PRINT( A) Print( #A + " = ", (A))

double MathNearest(  double v, double to){ return to * MathRound(v / to); }
double MathRoundDown(double v, double to){ return to * MathFloor(v / to); }
double MathRoundUp(  double v, double to){ return to * MathCeil( v / to); }

void OnStart()
  {
//--- testing edge cases for floor
//--- MathFloor(lots / step) * step always fails this test.
   PRINT(MathRoundDown(2.26, 0.01));     //2.25  
   PRINT(MathRoundDown(18.15, 0.01));    //8.14  
   PRINT(MathRoundDown(-9.13, 0.01));    //-9.14 
   PRINT(MathRoundDown(-65.18, 0.01));   //-65.19

//--- testing edge cases for ceil
//--- MathCeil(lots / step) * step always fails this test.
   PRINT(MathRoundUp(9.13, 0.01));        // 9.14
   PRINT(MathRoundUp(65.18, 0.01));       // 65.19
   PRINT(MathRoundUp(-2.26, 0.01));       //-2.25 
   PRINT(MathRoundUp(-18.15, 0.01));      //-18.14
  }

Simple and naïve functions, they don't take care of floating point roundoff errors.