Format

Format is a built-in scripting function that formats a numerical value and returns it as a string.

Function Group Execution Windows Embedded Thin Client
Format Arithmetic Synchronous Supported Supported Supported

Syntax

Format(strFlag,numValue)
strFlag
A description of how the given numerical value should be formated, according to the syntax %width.precisionFormat, where:
  • width is the minimum number of characters to be returned by the function. If the value to be returned is shorter than this, then it is padded with either blank spaces (" ") or zeroes ("0"); see examples below. The value is not truncated even if the result is longer than the specified width. Applicable for flags d, x, X, o, b, f, e, E, g, G, s, c and h.
  • .precision is the number of decimal places for a floating-point number. Applicable for flags f, e, E, g and G.
  • F is the specific format:
    Format Description
    d Decimal
    x Hexadecimal (alphabetic characters in lowercase)
    X Hexadecimal (alphabetic characters in uppercase)
    o Octal
    b Binary
    f Floating-point
    e Scientific notation (e in lowercase)
    E Scientific notation (E in uppercase)
    g Rounded (e in lowercase, when applicable)
    G Rounded (E in uppercase, when applicable)
    s String
    c ASCII character (i.e., the numerical value is interpreted as an ASCII character code)
    h Hour (hh:mm:ss)

Alternatively, the format can be set using the syntax ##.###, where the numerical value is rounded to the specified number of decimal places.

numValue
The numerical value to be formatted.

Returned value

This function returns a string that contains the formatted numerical value.

Notes

Format is similar to the printf function in other programming languages, and it allows most of the same formatting options. However, unlike printf, Format can be used to format only one numerical value at a time.

This function is particularly useful for formatting values to be printed in reports.

Examples

Tag Name Expression
Tag Format( "%d", 12.34 ) // Returned value = "12"
Tag Format( "%04d", 12.34 ) // Returned value = "0012"
Tag Format( "%4d", 12.34 ) // Returned value = "12"
Tag Name Expression
Tag Format( "%x", 26 ) // Returned value = "1a"
Tag Format( "%04x", 26 ) // Returned value = "001a"
Tag Format( "%4x", 26 ) // Returned value = "1a"
Tag Name Expression
Tag Format( "%X", 26 ) // Returned value = "1A"
Tag Format( "%04X", 26 ) // Returned value = "001A"
Tag Format( "%4X", 26 ) // Returned value = "1A"
Tag Name Expression
Tag Format( "%o", 16 ) // Returned value = "20"
Tag Format( "%04o", 16 ) // Returned value = "0020"
Tag Format( "%4o", 16 ) // Returned value = "20"
Tag Name Expression
Tag Format( "%b", 2 ) // Returned value = "10"
Tag Format( "%4b", 2 ) // Returned value = "0010"
Tag Format( "%04b", 2 ) // Returned value = "0010"
Tag Name Expression
Tag Format( "%0.1f", 12.34 ) // Returned value = "12.3"
Tag Format( "%06.1f", 12.34 ) // Returned value = "0012.3"
Tag Format( "%6.1f", 12.34 ) // Returned value = "12.3"
Tag Name Expression
Tag Format( "%e", 12.34 ) // Returned value = "1.234000e+001"
Tag Format( "%0.1e", 12.34 ) // Returned value = "1.2e+001"
Tag Format( "%09.1e", 12.34 ) // Returned value = "01.2e+001"
Tag Format( "%9.1e", 12.34 ) // Returned value = " 1.2e+001"
Tag Name Expression
Tag Format( "%E", 12.34 ) // Returned value = "1.234000E+001"
Tag Format( "%0.1E", 12.34 ) // Returned value = "1.2E+001"
Tag Format( "%09.1E", 12.34 ) // Returned value = "01.2E+001"
Tag Format( "%9.1E", 12.34 ) // Returned value = " 1.2E+001"
Tag Name Expression
Tag Format( "%0.1g", 12.34 ) // Returned value = "1e+001"
Tag Format( "%0.2g", 12.34 ) // Returned value = "12"
Tag Format( "%0.3g", 12.34 ) // Returned value = "12.3"
Tag Format( "%05.3g", 12.34 ) // Returned value = "012.3"
Tag Format( "%5.3g", 12.34 ) // Returned value = " 12.3"
Tag Name Expression
Tag Format( "%0.1G", 12.34 ) // Returned value = "1E+001"
Tag Format( "%0.2G", 12.34 ) // Returned value = "12"
Tag Format( "%0.3G", 12.34 ) // Returned value = "12.3"
Tag Format( "%05.3G", 12.34 ) // Returned value = "012.3"
Tag Format( "%5.3G", 12.34 ) // Returned value = "12.3"
Tag Name Expression
Tag Format( "%s", 12.34 ) // Returned value = "12"
Tag Format( "%04s", 12.34 ) // Returned value = "0012"
Tag Format( "%4s", 12.34 ) // Returned value = "12"
Tag Name Expression
Tag Format( "%c", 97 ) // Returned value = "a"
Tag Format( "%4c", 97 ) // Returned value = "a"
Tag Format( "%04c", 97 ) // Returned value = "000a"
Tag Name Expression
Tag Format( "%h", 30 ) // Returned value = "00:00:30"
Tag Format( "%h", 60 ) // Returned value = "00:01:00"
Tag Format( "%h", 90 ) // Returned value = "00:01:30"
Tag Format( "%h", 3600 ) // Returned value = "01:00:00"
Tag Name Expression
Tag Format( "##.#", 26.56789 ) // Returned value = "26.6"
Tag Format( "#.##", 26.56789 ) // Returned value = "26.57"
Tag Format( "##.##", 26.56789 ) // Returned value = "26.57"