StringToReal() Function
- Last UpdatedNov 23, 2018
- 1 minute read
In a script, you can convert a value contained in a string to a real value by using the StringToReal() function.
You can use this to read a value contained at the beginning of a string into a real tag for further mathematical operations.
Note: This function also supports the exponential notation and converts a string expression 1e+6 correctly to 1000000.
Syntax
result = StringToReal (string)
Parameters
string
A literal string, message tagname, or string expression.
Remarks
The function checks the first character of the string. If it is a number, it attempts to read this and the following characters as a real number until a non-numeric character is met. The function ignores leading spaces in the string.
To extract the first real number from a string (message tag mtag) that is not at the beginning and store it in the real tag rtag1, use the following script:
DIM i AS INTEGER;
DIM tmp AS INTEGER;
FOR i = 1 TO StringLen(mtag) {run variable i over the characters of mtag}
tmp = StringASCII(StringMid(mtag, i, 1)) - 48; {detect ASCII value}
IF (tmp>=0 AND tmp<10) THEN {if ASCII value represented "0" - "9"}
rtag = StringToReal(StringMid(mtag, i, 0)); {set rtag to value from that position and exit loop}
EXIT FOR;
ENDIF;
NEXT;
Example(s)
StringToReal("ABCD") returns 0.
StringToReal("13.4 mbar") returns 13.4.
StringToReal("Pressure is 13.4") returns 0.