Math Round upper and lower

 

Hi all, could you help me to round the number 1.13573 to 1.1360 ?

Once obtained the number, how to extract the number "6" ?

Thank you!

 

Codewise ?

Scan all the digits from right to left and if they are >=5 assign a +1 to the next digit.

MqlWise : MathRound() for auto rounding , MathCeil() for round up , MathFloor() for round down.

Unless You meant with a digit precision , in which case consult the following test code : 

enum RDS_operation
{
round_up=0,//Round Up (Ceil)
round_down=1,//Round Down (Floor)
round_auto=2//Round Auto 
};
input double feed=1.134;//feed value
input int round_digit=2;//digit
input RDS_operation operation=round_auto;//method 
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
  double nv=RoundDigitSpecific(feed,operation,round_digit);
  Print("Result : "+nv);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+

double RoundDigitSpecific(double original,RDS_operation roperation,int LastDigit)
{
double result=original;
ulong multiple=(ulong)MathPow(10,LastDigit);
double edit=original*multiple;
if(roperation==round_up) edit=MathCeil(edit);
if(roperation==round_down) edit=MathFloor(edit);
if(roperation==round_auto) edit=MathRound(edit);
edit=edit/multiple;
result=edit;
return(result);
}

int HowManyDecimals(double value)
{
int decs=0;
ulong assist=0;
ulong multiple=1;
int cand_decs=-1;
bool more_decimals=true;
while(more_decimals)
{
cand_decs++;
multiple=(ulong)MathPow(10,cand_decs);
assist=(ulong)(value*multiple);
double difference=value*multiple-assist;
if(difference==0)
  {
  decs=cand_decs;
  more_decimals=false;
  }
}
return(decs);
}
 
  1. Alberto Tortella: could you help me to round the number 1.13573 to 1.1360 ?
    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); }
    MathNearest(1.13573, 0.001) // 1.136
    

  2. Alberto Tortella: Once obtained the number, how to extract the number "6" ?
    No need for #1, just extract it
    (int)MathRound(1.13573 / 0.001) % 10;
    // 1.13573 / 0.001 = 1135.73
    // Round(1135.73) = 1136.00
    // (int)1136 = 1136
    // 1136 % 10 = 6

 

Thank you very much for the answers.

I have an error with the "% 10" code, illegal use of %. 

Could you help me? 

 
Alberto Tortella:

Thank you very much for the answers.

I have an error with the "% 10" code, illegal use of %. 

Could you help me? 

% == modulo

Try MathMod() function.

 
Alberto Tortella: I have an error with the "% 10" code, illegal use of %. Could you help me?

I did help you. My posted code (#2.2) compiles just fine (MT4/1170/strict.)

Reason: