Convert from a String to a Numeric Data Type in LabVIEW

Updated Nov 8, 2020

Environment

Software

  • LabVIEW

LabVIEW provides many built-in methods to convert between data types. When converting between string data and numeric, there are some considerations like your numeric datatype and the format of your input string. This tutorial provides options for converting string data to numeric datatypes in LabVIEW. 

String to Number Conversion Functions

There are four built-in string to number conversion functions in LabVIEW that convert string input data to its associated numeric output data types Determining which of these built-in functions you should use depends on your input data type. Below is an example of how to use one of these built-in functions.
 

Fract/Exp String To Number Function

The Fract/Exp String to Number function works well if you are using numbers containing decimal points or those using exponential values, but can also be used for integers. This method is available in LabVIEW 6.0 and later.
  1. On your LabVIEW block diagram, add the Fract/Exp String to Number function (Functions Palette » String » String/Number Conversion
  2. Right-click the number output node and select Create Indicator.
  3. Right-click on the numeric indicator and select Properties.
  4. Navigate to the Display Format tab to configure your preferred output formatting.
  5. Optional: If on your local system, a decimal separation is indicated by a comma, set the use system decimal point to true as seen in the snippet below.


This image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project.  To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW block diagram
 

Scan from String Function

Other than the standard built-in String/Number conversation functions, you can also use the Scan from String function to convert string data into multiple data types including binary integers, SI numbers, and timestamps. This method is available in LabVIEW 5.0 and later.
  1. On the LabVIEW block diagram, add the Scan from String function (Functions Palette » String).
  2. Right-click on the function and select Edit Scan String.
  3. Select the appropriate data type from the Selected operation drop-down menu that matches the data type of your input string.
  4. Customize the string format using the Corresponding scan string textbox. This tells the function what to look for in the input string and should match the format of the input string.
    • Use Format Specifier Syntax as guidance in setting the scan string format.
    • Tip: The pattern for specifying the decimal separator is the percent sign followed by the decimal separator you choose followed by a semi-colon. Which is %,;%f for the comma case as seen in the snippet below.


This image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project.  To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW block diagram