# Issue with StringToDouble()

495

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...
Moderator
10617

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.
495

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
Moderator
10617

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.
2950

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).
Moderator
2322

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)

Moderator
10617

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).

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.

Moderator
33490

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).
Moderator
10617

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.
Moderator
2322

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.

Moderator
2322

Keith Watford:

Seems that Honest Knave has already done some investigation.