Rounding numbers in MT4 via NormalizeDouble - page 8

 
Dmitry Fedoseev:

Well and wrong.

Subtract, normalise to two digits and then to one.

So try it :)

 
Event:

So try it :)

What is there to try? It's pretty clear. The subtraction result is non-normalised, so there will be surprises sometimes if you don't normalise to two digits.
 
Event:

Which one do you need?

you need to knowthe length of the string? - Are you sure you have the right branches?

Code?

If it's more than 2 lines like mine, then your code is more complicated.

 
Vasyl Nosal:

Code?

If it's more than 2 lines like mine, then your code is more complicated.

You're not reading your posts carefully.

I gave you the code - it has one line.

 
Event:

You are not reading the posts carefully.

I gave you the code - it has one line in it.

That's cool. I'll write 1.15 normalise in my code like that:)))))))))))))))))))))))))))))))))))))))))))))))))))
 
Vasyl Nosal:

+++

However, if a number is longer than 15 decimal places, it must still be truncated somehow, otherwise it won't work properly.

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   ushort  Characters_delete=15;
   for(int i=0;i<=Characters_delete;i++)
     {
      double v1=1.123456789012345;

      v1=DoubleCrop(v1,i,Characters_delete);

      Print("v1 = ",i," = ",DoubleToString(v1,Characters_delete-i));
     }
   Print("---");
  }
//+------------------------------------------------------------------+
//| Double Crop function                                             |
//+------------------------------------------------------------------+
double DoubleCrop(double value,int ii,ushort  Characters)
  {
   string data="",resoult="";
   data=DoubleToString(value,Characters);
   resoult=StringSubstr(data,0,StringLen(data)-ii);
   return StringToDouble(resoult);
  }
//+------------------------------------------------------------------+
 
Event:

So try it :)

With zero and with negative numbers wrong. With positive ones, surprisingly, without any surprises, correct.
 

Vasyl Nosal:

extern ushort Characters_delete = 1;

///////////////////////////////////////////
string data=DoubleToString(Bid,Digits);
string resoult=StringSubstr(data,0,StringLen(data)-Characters_delete);  

Heh. That's the wrong way round. Totally wrong results.
 
Dmitry Fedoseev:
It's wrong with zero and with negative numbers. With positive ones, surprisingly, without surprises, it's correct.

Negative ones should be added to (if Normalize increases modulo when rounding).

And probably do nothing with zero.

If applied to a quote - its probability to become <= 0 is small... :)

 
Eight pages about rounding. I think that used to be covered in fourth grade :(