Need help, how to get the last number of a bigger number?

To add comments, please log in or register
Mrluck07
425
Mrluck07  

Hi, i'm having a problem, i need to make a variable to get the last number from a bigger number:


example:


double a= 10003;

double b= // this double should get the last number of "a", so it should =3. How can i do this?

Thanks for any help

Gyunay Sali
6018
Gyunay Sali  
Mrluck07:

Hi, i'm having a problem, i need to make a variable to get the last number from a bigger number:


example:


Thanks for any help

Hello, one easy way is to convert double to string using the function DoubleToString() and then to get the last character of the string with StringGetChar(). To find the string length use StringLen() function. When you find the last character then you can convert it again to double with StringToDouble().

Perhaps there is another way. MQL language is very flexible. But the above solution will work i am sure.

Mrluck07
425
Mrluck07  
Gyunay Sali:

Hello, one easy way is to convert double to string using the function DoubleToString() and then to get the last character of the string with StringGetChar(). To find the string length use StringLen() function. When you find the last character then you can convert it again to double with StringToDouble().

Perhaps there is another way. MQL language is very flexible. But the above solution will work i am sure.

Hi, i tried what you explained:


double number= 1002; // 

string numberstring= DoubleToStr(number); // convert in a string

string numberstring2=StringGetChar(numberstring,4); //get the forth value

Comment (numberstring2); 

But don't get the correct number

Gyunay Sali
6018
Gyunay Sali  

Hello again,

The first character from a string has position 0. This means that you have to use:

string numberstring2=StringGetChar(numberstring,3); //get the forth value

To get the last character.

Stefan Winter
1303
Stefan Winter  
Mrluck07:

Hi, i'm having a problem, i need to make a variable to get the last number from a bigger number:


example:


Thanks for any help

You need the modulo function. 1002 % 10 = 2 or you can use MathMod().

Mrluck07
425
Mrluck07  

Hi, i can't make it work, i need a code example, can someone give me the complete code?

double a=1002

so please what should i code next to get the last number?

double b= 2?

Mikhail Zhitnev
19140
Mikhail Zhitnev  
1) Translate number to string
2) Get the length of string.
3) Extract the last char of string to the new one string (length-1)
4) Translate last char to the number
Mikhail Zhitnev
19140
Mikhail Zhitnev  
String st="1004";
int a=length(st);
string z = StrSubstr(st, a-1);
int res=StrToInt(z);
Mrluck07
425
Mrluck07  
Mikhail Zhitnev:
1) Translate number to string
2) Get the length of string.
3) Extract the last char of string to the new one string (length-1)
4) Translate last char to the number
1- 

double number= 1004;
string numberstring= DoubleToStr(number);

2- string numberstring2=StringLen(numberstring);

3- string numberstring3 = StringGetChar( numberstring2,4);

4- Comment (numberstring3);


Doesn't work, how can i solve this?

Carl Schreiber
Moderator
7244
Carl Schreiber  

not tested:

double a= 10003;
int lastNum = ((int)a)%10;
Mrluck07
425
Mrluck07  
Carl Schreiber:

not tested:

Great, it worked! Thanks a lot Sir
To add comments, please log in or register