Issue with StringToDouble()
The second one only contains the number 2 and the rest are letters.
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
The function converts string containing a symbol representation of number into number of double type.
EDIT: Looks like @Keith Watford posted an answer at the same time (and just before).
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
| String | Double | Reason |
|---|---|---|
| "x12345" | 0.0 | First character was a letter |
| "123x45" | 123.0 | Encountered a letter after 123 |
| " 123" | 123.0 | Whitespace ignored at start of string |
| " 12 3" | 12.0 | Encountered whitespace after 12 |
| "123.45" | 123.45 | One decimal point is OK |
| "123.4.5" | 123.4 | Encountered a second decimal point |
| "1,234.50" | 1.0 | Encountered a comma |
Not documented (to my knowledge)
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.
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
| String | Double | Reason |
|---|---|---|
| "x12345" | 0.0 | First character was a letter |
| "123x45" | 123.0 | Encountered a letter after 123 |
| " 123" | 123.0 | Whitespace ignored at start of string |
| " 12 3" | 12.0 | Encountered whitespace after 12 |
| "123.45" | 123.45 | One decimal point is OK |
| "123.4.5" | 123.4 | Encountered a second decimal point |
| "1,234.50" | 1.0 | Encountered a comma |
Not documented (to my knowledge)
Well done, a great answer.
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
| String | Double | Reason |
|---|---|---|
| "x12345" | 0.0 | First character was a letter |
| "123x45" | 123.0 | Encountered a letter after 123 |
| " 123" | 123.0 | Whitespace ignored at start of string |
| " 12 3" | 12.0 | Encountered whitespace after 12 |
| "123.45" | 123.45 | One decimal point is OK |
| "123.4.5" | 123.4 | Encountered a second decimal point |
| "1,234.50" | 1.0 | Encountered a comma |
Not documented (to my knowledge)
Each time I am using StringToDouble() it's because I know there is a double inside the string (from a CSV file for example).
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).
Apart from that I don't think that I use it.
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.
Seems that Honest Knave has already done some investigation.
Well done, a great answer.
Moreover, why does the function
and so on...