The most simple way is a typecast or use StringToDouble().
But you need to be aware of the way that strings get typecasted into numbers i.e.
Forum on trading, automated trading systems and testing trading strategies
honest_knave, 2017.02.25 11:30
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)
If you're looking to extract certain numbers like sscanf does, that will definitely require your own function.
You may find some ideas in here or here. It will depend on how you expect your string to be formatted, and what you want to extract.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi,
I'm looking for a function that can retrieve numeric values from a formatted string.
MQL has functions like printf(), StringFormat, etc, to format values to string, but I just can not find a "formatted input" function, i.e., like C's "sscanf()".
Anyone has a clue? Thanks!