2.6.3 Converting Strings to Numbers

The next few examples demonstrate converting a string of numeric characters to an actual number.

Converting String to Numeric

Using Substitution Notation

To convert a variable of type string to a variable of type numeric (double, int, const), consider the following simple example:

// myString contains the characters 456 as a string
string myString$ = "456";         

// myStringNum now contains the integer value 456
int myStringNum = %(myString$);

The syntax %(string$) is one of the substitution notations supported in LabTalk. Another, $(num), is used to convert in the opposite direction; from numeric to string.

Using String Registers

This example demonstrates how to convert a string held in a string register to a numeric value.

// Similar to above, but performed using string registers:
string myString$ = "456"; 
// Assignment without quotes will evaluate the right-hand-side
%A = myString$;
// %A will be substituted, then right-hand-side evaluated
int aa = %A;     
// 'aa' can be operated on by other integers
int bb = aa + 100;    
bb=;                 // ANS: 556