the function of expressing ,(Comma) every thousand (cicode & ciVBA)

Hi, Everyone. I'm Kang.

I'm testing the function of expressing ,(Comma) every thousand in analog data.

Through several samples, I checked the functionality.

However, I've to use this function in Super Genie.

I created a function that uses ciVBA in cicode.
The return value of this function does not work normally, so it is not displayed on the screen.

Could you please help me if I have written something wrong

STRING
FUNCTION Test_Format(STRING sTag)

STRING sInput ;

STRING sOutput ;

REAL rPE = TagRead(sTag);

sInput = rPE ;
sOutput = VbCallReturn(VbCallRun(VbCallOpen("FormatTheReal", sInput)));
RETURN sOutput;

END

Function FormatTheReal (Byval iInVal As Single) As string
Dim sFormattedValue As String
Dim iTagValue As Single
iTagValue = iInVal
sFormattedValue = Format (iTagValue, "Standard")
FormatTheReal = sFormattedValue
End Function

  • Just define the format of the variable tag. It will display correctly on the screen and also on popups. Otherwise you can use for instance something like this on pages: Tag1:##.#
  • Hi, Bas.

    I used the 'Test_Format' function in value display - numberic proterty.

    Also, I have defined the format of all variables.
    But I still don't see the return value of this function on the runtime screen.

    Have you checked using the Test_Format function?
  • Hi Kang,

    Your formatting function is blocking because of the TagRead command.
    Citect does not allow blocking functions to be used in graphics pages as foreground animation expressions.

    Please remove the TagRead call and pass the value of your tag to your Test_format function instead of the tag name.

    Also, I'm not sure if calls to VBA are also blocking or not. If so, you have to program this functionality using cicode only.

    Best regards,
    Patrick
  • It seems simple to use the VB format function to do the conversion, but it is better to just use Cicode. Here's a function I wrote to do the same thing in just Cicode. It is designed to be able to be called from a graphics expression.

    // ValToCDel function
    //
    // Converts an INT, UINT, LONG, or REAL value to a Comma-Delimited-Value
    // (a string with commas as thousands separators)
    //
    // Arguments: 	sInput	Input value
    //		iMode	0 Value is INT, UINT, or LONG (Default)
    //			1 Value is REAL
    //
    // Return Value: String with commas
    //
    // Example: String Expression (on a graphic page): ValToCDel(IntTag)
    // If IntTag is -12345, then the string will display as: -12,345
    //
    // For a number with decimals, you have to use mode 1. Example: ValToCDel(AnalogTag, 1)
    //
    STRING FUNCTION ValToCDel(STRING sInput, INT iMode = 0)
    
    	STRING sOutput;		// Output value
    	STRING sPrefix;		// Holds "-" if needed
    	STRING sSuffix;		// Holds decimal places if needed
    
    	SELECT CASE iMode
    		CASE 0	// Integer
    			sInput = StrToInt(sInput);	// Strip off decimals (default format adds .00)
    		CASE 1	// Real
    			sSuffix = StrRight(sInput, StrLength(sInput) - StrSearch(0, sInput, "."));	// Copy decimals into separate string
    			sInput = StrToInt(StrLeft(sInput, StrSearch(0, sInput, ".")));				// Strip decimals off input string
    	END SELECT
    
    	IF StrToInt(sInput) < 0 THEN		// If input string is negative, move "-" into separate string
    		sPrefix = "-";
    		sInput = StrRight(sInput, StrLength(sInput) - 1);
    	END
    
    	WHILE sInput <> "" DO
    		sOutput = StrRight(sInput, 1) + sOutput;			// Copy 1 char from input string to output string
    		sInput = StrLeft(sInput, StrLength(sInput) - 1);	// Delete last char from input string
    
    		IF ((StrLength(sOutput) + 1) MOD 4 = 0) AND (StrLength(sInput) >= 1) THEN		// IF output string length is a multiple of 4 (3 digits + comma) and input string still has more chars THEN
    			sOutput = "," + sOutput;							// Add comma
    		END
    	END
    
    	sOutput = sPrefix + sOutput + sSuffix;		// Add "-" and decimal places if used
    
    	RETURN sOutput;
    
    END