Search code examples
vbscriptlabview

Displaying/Formatting Output in Exponential Form in VBScript


I'm working on some automation project.

The Data is passed in LabVIEW form a third party hardware, and is further passed to WinCC Flexible via a OPC server in float datatype.

The output display field supports string output. The data is displayed on the display field is processed in the VBScript.

The problem being faced is :

WinCC support float tags and has a maximum length of 4.

The output to be displayed on Display Field is of type string. When the data is passed through LabVIEW following happens:

Example 1: LabVIEW Data : 1.27e-4 | Output on WinCC Flex : 0.000127 [Wrong Representation] All the data below e-5 are represented like above.

Example 2:
LabVIEW Data : 1.27e-10 | Output on WinCC Flex : 1.27E-10 [Correct Rep.]

Is there any way in VBScript to format the output data into exponential notation?

Currently am using this VBS for the representation...

 If SmartTags("tag_06_1") = 0 Then SmartTags("output_1") = CStr (SmartTags("presseure_test"))

Solution

  • The best (most bang for the buck (bug?)) solution to all formatting problems in VBScript is to harness .NET formatting. Simple POC script:

      Dim aNums : aNums = Split("0.123 1.27e-4 1.27e-10")
      Dim sNum
      For Each sNum in aNums
          WScript.Echo sNum, CDbl(sNum), fmtExpNum(CDbl(sNum))
      Next
    
    Function fmtExpNum(dblX)
      Dim oSB : Set oSB = CreateObject("System.Text.StringBuilder")
      oSB.AppendFormat "{0:E2}", dblX
      fmtExpNum = oSB.ToString()
    End Function
    

    output (german locale):

    0.123 0,123 1,23E-001
    1.27e-4 0,000127 1,27E-004
    1.27e-10 0,000000000127 1,27E-010