Download MetaTrader 5

Issue with StringToDouble()

To add comments, please log in or register
macpee
495
macpee  
Why does the function
StringToDouble("1st Actual");
return the value 1.00000000, instead of 0.00000000?

Moreover, why does the function

StringToDouble("2nd Actual");
return 2.00000000, instead of 0.00000000 ?

and so on...
Keith Watford
Moderator
10617
Keith Watford  
Probably because the first one only contains the number 1 whereas the others are letters.

The second one only contains the number 2 and the rest are letters.
macpee
495
macpee  
Keith Watford:
Probably because the first one only contains the number 1 whereas the others are letters.

The second one only contains the number 2 and the rest are letters.
Yes, but both inputs are strings and it they should return zero values
Keith Watford
Moderator
10617
Keith Watford  
macpee:
Yes, but both inputs are strings and it they should return zero values
Parameters have to be strings for StringToDouble()

The function converts string containing a symbol representation of number into number of double type.


Both your strings contain a symbol representation of number.
Fernando Carreiro
2950
Fernando Carreiro  
macpee: Yes, but both inputs are strings and it they should return zero values
As @Keith Watford, pointed out, the strings start with numerical digits, so the assumption is that in converts the initial part as that of a valid number and disregards the rest.

EDIT: Looks like @Keith Watford posted an answer at the same time (and just before).
honest_knave
Moderator
2322
honest_knave  

StringToDouble (and StrToDouble) will start from the left-most character.

  • It will skip whitespace at the start of a string, until a different character is encountered.
  • It will interpret one '+' or '-' as a positive or negative sign, but only when it is the first non-whitespace character encountered.
  • It will interpret number(s) in their literal sense.
  • It will interpret one '.' as a decimal point. 
  • As soon as it encounters whitespace after a non-whitespace character, or a 2nd decimal point, or a '+' or '-' sign that is not the first non-whitespace character, or any other character that is not a number it will disregard the rest of the string.
  • Note: it disregards the rest of the string, not the entire string.

Examples

StringDoubleReason 
"x12345"0.0First character was a letter
"123x45"123.0Encountered a letter after 123 
"    123"123.0Whitespace ignored at start of string 
"   12 3"12.0Encountered whitespace after 12
"123.45"123.45One decimal point is OK 
"123.4.5"123.4Encountered a second decimal point 
 "1,234.50"1.0Encountered a comma 

Not documented (to my knowledge)

Keith Watford
Moderator
10617
Keith Watford  
Fernando Carreiro:
As @Keith Watford, pointed out, the strings start with numerical digits, so the assumption is that in converts the initial part as that of a valid number and disregards the rest.

EDIT: Looks like @Keith Watford posted an answer at the same time (and just before).
Thanks, I didn't realise that it actually had to start with a number as a string.

Of course macpee should have investigated and done some tests.

honest_knave:

StringToDouble (and StrToDouble) will start from the left-most character.

  • It will skip whitespace at the start of a string, until a different character is encountered.
  • It will interpret number(s) in their natural sense.
  • It will interpret one '.' as a decimal point. 
  • As soon as it encounters whitespace after a number/decimal point, or a 2nd decimal point, or any other character that is not a number it will disregard the rest of the string.
  • Note: it disregards the rest of the string, not the entire string.

Examples

StringDoubleReason 
"x12345"0.0First character was a letter
"123x45"123.0Encountered a letter after 123 
"    123"123.0Whitespace ignored at start of string 
"   12 3"12.0Encountered whitespace after 12
"123.45"123.45One decimal point is OK 
"123.4.5"123.4Encountered a second decimal point 
 "1,234.50"1.0Encountered a comma 

Not documented (to my knowledge)

Seems that Honest Knave has already done some investigation.

Well done, a great answer.
Alain Verleyen
Moderator
33490
Alain Verleyen  
honest_knave:

StringToDouble (and StrToDouble) will start from the left-most character.

  • It will skip whitespace at the start of a string, until a different character is encountered.
  • It will interpret number(s) in their natural sense.
  • It will interpret one '.' as a decimal point. 
  • As soon as it encounters whitespace after a number/decimal point, or a 2nd decimal point, or any other character that is not a number it will disregard the rest of the string.
  • Note: it disregards the rest of the string, not the entire string.

Examples

StringDoubleReason 
"x12345"0.0First character was a letter
"123x45"123.0Encountered a letter after 123 
"    123"123.0Whitespace ignored at start of string 
"   12 3"12.0Encountered whitespace after 12
"123.45"123.45One decimal point is OK 
"123.4.5"123.4Encountered a second decimal point 
 "1,234.50"1.0Encountered a comma 

Not documented (to my knowledge)

I am wondering what could be a real world usage of this possibility of the function ? I never use it.

Each time I am using StringToDouble() it's because I know there is a double inside the string (from a CSV file for example).
Keith Watford
Moderator
10617
Keith Watford  
Alain Verleyen:
I am wondering what could be a real world usage of this possibility of the function ? I never use it.

Each time I am using StringToDouble() it's because I know there is a double inside the string (from a CSV file for example).
Yes, I use it when reading from files and also getting a value from OBJ_EDIT.

Apart from that I don't think that I use it.
honest_knave
Moderator
2322
honest_knave  
Alain Verleyen:
I am wondering what could be a real world usage of this possibility of the function ? I never use it.

Each time I am using StringToDouble() it's because I know there is a double inside the string (from a CSV file for example).

It doesn't really achieve anything beyond a simple typecast.

Like @Keith Watford I use it in conjunction with OBJ_EDIT boxes. But I opt for the simple typecast rather than the function call.

honest_knave
Moderator
2322
honest_knave  
Keith Watford:

Seems that Honest Knave has already done some investigation.

Well done, a great answer.
Thanks, I had to modify my original post as I forgot about + / - 
123
To add comments, please log in or register