convert custom price into array EX: 124.53 ==> {1,2,4,5,3}

To add comments, please log in or register
Ahmed Farag
8710
Ahmed Farag  

Hello,


How to convert any price from this style xxx.xxx to numbers in the array?


I want to convert this steps into a mql4 code ???

for EX: the price is 1.25245

1- price/Point;   ==> 125245 (done)

2- 125245   ==> array {1,2,5,2,4,5} (need from this step)

3- 1+2+5+2+4+5 = 19

4- 19  = 1+9 = 10

5- 10 = 1+0 = 1

6- 1.25245 = 1


we converting any price to number from 1 to 9


Thanks

Ahmed Farag
8710
Ahmed Farag  
Jack Thomas:


Your user profile says you have 6 years of professional coding experience.
If that's true, then you should understand exactly how trivial this problem is to resolve.

Convert the price to a string.

Loop through the substring positions of the price string by 1 position at a time.

Assign that value from the position into the corresponding array position.

You should be able to do it in 5 lines of code or less.

There are other ways to do it as well....

You're welcome.

Thanks

Amir Yacoby
1262
Amir Yacoby  
Ahmed Farag:

Hello,


How to convert any price from this style xxx.xxx to numbers in the array?


I want to convert this steps into a mql4 code ???

for EX: the price is 1.25245

1- price/Point;   ==> 125245 (done)

2- 125245   ==> array {1,2,5,2,4,5} (need from this step)

3- 1+2+5+2+4+5 = 19

4- 19  = 1+9 = 10

5- 10 = 1+0 = 1

6- 1.25245 = 1


we converting any price to number from 1 to 9


Thanks

No need for array, if what you look is the sum of digits.

void OnStart()
  {
   double price=1.25245;
   Print(SumOfDigits(price));
  }
int SumOfDigits(double price)
  {
   int p=DoubleToString(price/_Point);
   return (p==0 ? 0 : p%9==0 ? 9 : p%9);
  }
Ahmed Farag
8710
Ahmed Farag  
Amir Yacoby:

No need for array, if what you look is the sum of digits.

yes,

Very thanks

Ahmed Farag
8710
Ahmed Farag  
Jack Thomas:

you asked for an array.

The following code will take a price and place it into an array, using the StringSplit() function.

All of your data is in the temp[] array.

There are NUMEROUS methods you can use to do it.


https://www.mql5.com/en/docs/strings/stringsplit

This way is better Jack

only two lines of code

Amir Yacoby
1262
Amir Yacoby  
Jack Thomas:

you asked for an array.

The following code will take a price and place it into an array, using the StringSplit() function.

All of your data is in the temp[] array.

There are NUMEROUS methods you can use to do it.


https://www.mql5.com/en/docs/strings/stringsplit

The goal for OP was to get the sum of digits of price into one digit. For this, there is no need for array. 
Ahmed Farag
8710
Ahmed Farag  
Amir Yacoby:
The goal for OP was to get the sum of digits of price into one digit. For this, there is no need for array. 

Yes U are right.

Nikolai Semko
4842
Nikolai Semko  
Ahmed Farag:

 1.25245 = 1

recursion will help

void OnStart()
  {
   double price = 1.25245;
   uint N=Num((uint)fabs(round(price/_Point)));
   Print(string(N));
  }

uint Num(uint x)
  {
   uint s=0;
   while(x!=0) {s+=x%10; x=int(x/10);}
   if (s>9) s=Num(s); 
   return(s);
  }
Ahmed Farag
8710
Ahmed Farag  
Nikolai Semko:

recursion will help

yes it will help

thanks 

Nikolai Semko
4842
Nikolai Semko  
Amir Yacoby:

No need for array, if what you look is the sum of digits.

return (p==0 ? 0 : p%9==0 ? 9 : p%9);

Brilliantly!

Your variant is much better than mine.

I did not understand at once. :))

To add comments, please log in or register