Rounding numbers in MT4 via NormalizeDouble - page 9

 
Алексей Тарабанов:
Eight pages about rounding. I think we used to learn that in the fourth grade :(
There's already some talk about circumcision :)
 
Алексей Тарабанов:
Eight pages about rounding. I think we used to learn that in the fourth grade :(
Not all of them ;|
 
Dmitry Fedoseev:
Heh. That's the wrong way round. Totally wrong results.
Like what?
 
lilita bogachkova:

+++

However, if a number is longer than 15 decimal places, it still needs to be trimmed somehow, otherwise it doesn't work properly.

Double-precision floating point numbers are accurate to 15-17 decimal places
 
Vasyl Nosal:
Double precision floating point numbers are accurate to 15-17 decimal digits
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   ushort  Characters_delete=14;
   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);
  }
//+------------------------------------------------------------------+
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 0 = 1.12345678901235
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 1 = 1.1234567890123
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 2 = 1.123456789012
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 3 = 1.12345678901
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 4 = 1.1234567890
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 5 = 1.123456789
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 6 = 1.12345678
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 7 = 1.1234567
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 8 = 1.123456
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 9 = 1.12345
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 10 = 1.1234
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 11 = 1.123
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 12 = 1.12
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 13 = 1.1

0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 14 = 1

   ushort  Characters_delete=15;

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 0 = 1.123456789012345

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 1 = 1.12345678901234

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 2 = 1.1234567890123

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 3 = 1.123456789012

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 4 = 1.12345678901

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 5 = 1.1234567890

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 6 = 1.123456789

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 7 = 1.12345678

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 8 = 1.1234567

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 9 = 1.123456

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 10 = 1.12345

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 11 = 1.1234

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 12 = 1.123

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 13 = 1.12

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 14 = 1.1

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 15 = 1

   ushort  Characters_delete=15;
   for(int i=0;i<=Characters_delete;i++)
     {
      double v1=1.1234567890123456;

      v1=DoubleCrop(v1,i,Characters_delete);

      Print("v1 = ",i," = ",DoubleToString(v1,Characters_delete-i));
     }
   Print("---");
  }

0 22:02:48.826 ! tests GBPUSDbc,H1: v1 = 0 = 1.123456789012346

0 22:02:48.826 ! tests GBPUSDbc,H1: v1 = 1 = 1.12345678901234

0 22:02:48.826 ! tests GBPUSDbc,H1: v1 = 2 = 1.1234567890123

0 22:02:48.826 ! tests GBPUSDbc,H1: v1 = 3 = 1.123456789012

0 22:02:48.826 ! tests GBPUSDbc,H1: v1 = 4 = 1.12345678901

0 22:02:48.826 ! tests GBPUSDbc,H1: v1 = 5 = 1.1234567890

0 22:02:48.826 ! tests GBPUSDbc,H1: v1 = 6 = 1.123456789

0 22:02:48.826 ! tests GBPUSDbc,H1: v1 = 7 = 1.12345678

0 22:02:48.826 ! tests GBPUSDbc,H1: v1 = 8 = 1.1234567

0 22:02:48.826 ! tests GBPUSDbc,H1: v1 = 9 = 1.123456

0 22:02:48.826 ! tests GBPUSDbc,H1: v1 = 10 = 1.12345

0 22:02:48.826 ! tests GBPUSDbc,H1: v1 = 11 = 1.1234

0 22:02:48.826 ! tests GBPUSDbc,H1: v1 = 12 = 1.123

0 22:02:48.826 ! tests GBPUSDbc,H1: v1 = 13 = 1.12

0 22:02:48.826 ! tests GBPUSDbc,H1: v1 = 14 = 1.1

0 22:02:48.826 ! tests GBPUSDbc,H1: v1 = 15 = 1

 
lilita bogachkova:
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 0 = 1.12345678901235
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 1 = 1.1234567890123
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 2 = 1.123456789012
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 3 = 1.12345678901
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 4 = 1.1234567890
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 5 = 1.123456789
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 6 = 1.12345678
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 7 = 1.1234567
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 8 = 1.123456
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 9 = 1.12345
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 10 = 1.1234
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 11 = 1.123
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 12 = 1.12
0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 13 = 1.1

0 21:59:20.485 ! tests GBPUSDbc,H1: v1 = 14 = 1

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 0 = 1.123456789012345

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 1 = 1.12345678901234

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 2 = 1.1234567890123

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 3 = 1.123456789012

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 4 = 1.12345678901

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 5 = 1.1234567890

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 6 = 1.123456789

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 7 = 1.12345678

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 8 = 1.1234567

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 9 = 1.123456

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 10 = 1.12345

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 11 = 1.1234

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 12 = 1.123

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 13 = 1.12

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 14 = 1.1

0 22:01:25.346 ! tests GBPUSDbc,H1: v1 = 15 = 1

И?
 
Vasyl Nosal:
И?
string data=DoubleToString(1.123456789012345,15); 
int lenth=StringLen(data);
data=StringSubstr(data,0,lenth);


Alert(data);

2016.02.25 22:06:56.781 prob EURUSD,H1: Alert: 1.123456789012345

I've discounted that no more than 17 characters dubbed. What else do you need?


 
lilita bogachkova:
There's already some talk of circumcision here :)
In early 1980s, Antonina Loseva, a graduate of Noginsk programming school, was working under my guidance. Here is how she solved the problem of rounding to integers: int A = double B + 0.5
 
Vasyl Nosal:
For example?

DoubleToStr(0.9999,3)=1.000

So, draw your conclusions.

 
Vasyl Nosal:

2016.02.25 22:06:56.781 prob EURUSD,H1: Alert: 1.123456789012345

I've discounted that no more than 17 characters dubbed. What else do you need?


Try 14 and explain why

void OnStart()
  {
   ushort  Characters_delete=14;
   for(int i=0;i<=Characters_delete;i++)
     {
      double v1=1.0/1.1;

      v1=DoubleCrop(v1,i,Characters_delete);

      Print("v1 = ",i," = ",DoubleToString(v1,Characters_delete-i));
     }
   Print("---");
  }

0 22:15:05.379 ! tests GBPUSDbc,H1: v1 = 0 = 0.909090909091

0 22:15:05.379 ! tests GBPUSDbc,H1: v1 = 1 = 0.90909090909

0 22:15:05.379 ! tests GBPUSDbc,H1: v1 = 2 = 0.909090909090

0 22:15:05.379 ! tests GBPUSDbc,H1: v1 = 3 = 0.909090909

0 22:15:05.379 ! tests GBPUSDbc,H1: v1 = 4 = 0.9090909090

0 22:15:05.379 ! tests GBPUSDbc,H1: v1 = 5 = 0.909090909

0 22:15:05.379 ! tests GBPUSDbc,H1: v1 = 6 = 0.90909090

0 22:15:05.379 ! tests GBPUSDbc,H1: v1 = 7 = 0.9090909

0 22:15:05.379 ! tests GBPUSDbc,H1: v1 = 8 = 0.909090

0 22:15:05.379 ! tests GBPUSDbc,H1: v1 = 9 = 0.90909

0 22:15:05.379 ! tests GBPUSDbc,H1: v1 = 10 = 0.9090

0 22:15:05.379 ! tests GBPUSDbc,H1: v1 = 11 = 0.909

0 22:15:05.379 ! tests GBPUSDbc,H1: v1 = 12 = 0.90

0 22:15:05.379 ! tests GBPUSDbc,H1: v1 = 13 = 0.9

0 22:15:05.379 ! tests GBPUSDbc,H1: v1 = 14 = 0