Hi,
I’ve got some variables
Int var1= 12.598
Int var2 = 96.325
Int var3=12544.3658882
Int var4=9865.010144
How can I extract the last 2 characters on the right (98 , 25 or 82 etc.. )
With the function « StringSubstr »?
Because StringSubstr (var1,0,2) =12
Thanks for all your help!
Second, any numbers stored into an integer variables will be truncated, removing the fraction.
So you should use something like below:
double dVar = 12544.3658882; string sVar = StringConcatenate( dVar ); string subSVar = StringSubstr( sVar, StringLen(sVar)-2 ); Print( subSVar );
Hi,
I’ve got some variables
Int var1= 12.598
Int var2 = 96.325
Int var3=12544.3658882
Int var4=9865.010144
How can I extract the last 2 characters on the right (98 , 25 or 82 etc.. )
With the function « StringSubstr »?
Because StringSubstr (var1,0,2) =12
Thanks for all your help!
Second, any numbers stored into an integer variables will be truncated, removing the fraction.
So you should use something like below:
double dVar = 12544.3658882; string sVar = StringConcatenate( dVar ); string subSVar = StringSubstr( sVar, StringLen(sVar)-2 ); Print( subSVar );
Good thinking, man. Only one mistake in the substring function, which has three parameters, so I think the correct operation is as follows:
string subSVar = StringSubstr( sVar, (StringLen(sVar)-2), 2 );
Not really. Without the 3rd parameter means taking the rest of the string. Try it.
My mistake :).
Hi
Thanks you for your help
But I've got the message below
")" Wrong parameter count
// for string sVar = StringConcatenate( dVar );
How can I do please
Best regards
Sorry, try this:
string sVar = "" + dVar;
Hi
The result is : 00
if I write "string subSVar = StringSubstr( sVar, StringLen(sVar)-6 );"
the result is : 590000
Why?
Best Regards
Howdy natsirte
how bout this?
seems bit long winded but getting at digits is fiddly imho. MQL4 casts double to destination string with 8 string digits after decimal point. The loop starts at right moving left long as '0' seen. It will stop when only 2 chars are left to look at (as you wanted rightmost 2, seemed place to cut losses and run :) If sees any char other than '0' it gives up. The if(..) at end decides if can return substring containing the next 2 leftmost digits. Not completely foolproof but could be basis for ideas?
Seems totally over the top but is best I could come up with - it works... and guess that might mean something - lol
string sfGet2RightMostDigits(double d)
{
string s = d;
int iPos = StringLen(s)-1;
int iChar;
while( iPos >= 1 )
{
iChar = StringGetChar(s,iPos);
if( iChar != '0' ) break;
iPos--;
}
if( iPos >= 1 && iChar != '.' && StringGetChar(s,iPos-1) != '.' )
return( StringSubstr(s,iPos-1,2) );
return("");
}
//------------------------------
int start()
{
//some numbers - anything valid can be tested out...
double da[]={12.598,96.325,12544.3658882,9865.010144,1.23,12345.67890123,1.2,23.0};
for(int i=0;i<ArraySize(da);i++)
{
string s = da[i];
//log number as string + (2 rightmost digits OR empty string)
Print(s,", \"",sfGet2RightMostDigits(da[i]),"\"");
}
return(0);
}
edit forgot to include test o/p:
2007.09.15 00:14:31 testBed GBPUSD,M15: removed
2007.09.15 00:14:31 testBed GBPUSD,M15: 23.00000000, ""
2007.09.15 00:14:31 testBed GBPUSD,M15: 1.20000000, ""
2007.09.15 00:14:31 testBed GBPUSD,M15: 12345.67890123, "23"
2007.09.15 00:14:31 testBed GBPUSD,M15: 1.23000000, "23"
2007.09.15 00:14:31 testBed GBPUSD,M15: 9865.01014400, "44"
2007.09.15 00:14:31 testBed GBPUSD,M15: 12544.36588820, "82"
2007.09.15 00:14:31 testBed GBPUSD,M15: 96.32500000, "25"
2007.09.15 00:14:31 testBed GBPUSD,M15: 12.59800000, "98"
2007.09.15 00:14:31 testBed GBPUSD,M15: loaded successfully
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I’ve got some variables
Int var1= 12.598
Int var2 = 96.325
Int var3=12544.3658882
Int var4=9865.010144
How can I extract the last 2 characters on the right (98 , 25 or 82 etc.. )
With the function « StringSubstr »?
Because StringSubstr (var1,0,2) =12
Thanks for all your help!