Converting a String to an Integer Using LabVIEW Mathscript Node

Updated Sep 5, 2023

Reported In

Software

  • LabVIEW MathScript Module
  • LabVIEW

Issue Details

I am using the MathScript Node in LabVIEW and I'm trying to convert a string into an integer. However, the str_to_num function is returning a complex double data type.

Solution

Note: NI does not recommend LabVIEW MathScript Module functions for new designs. Refer to www.ni.com/migratemathscript  for information and recommended alternatives.

You can convert a string into an integer by using a combination of type conversion functions in the MathScript node, as shown below:
string = '1986'
real_number = real(str_to_num(string))
real_integer = int16(real_number(1))
Below is a screenshot of what this would look like in LabVIEW.

Additional Information

  • str_to_num returns a 1-dimensional (1D) complex double array with one element, the converted numeric value of string
  • real returns the real portion of the complex double array into a real double-precision 1D array with one element
  • int16 converts the double-precision value of its input, which is the first element of real_number
  • The value of real_integer is now a signed 16-bit integer, 1986
LabVIEW determines the data types of MathScript Node variables during edit-time, and automatically selects the smallest data type that encompasses all possible values of the variable. Since it is possible that a string would contain a complex double-precision number, that is the data type that the str_to_num and str_to_double functions return by default.