FORMAT
Use the FORMAT script function to format numbers by adding or removing leading zeros, or to control the number of decimal places in floating-point numbers.
Syntax
FORMAT (number [, format])
Parameters
| Parameter | Description | Format | Default Value |
|---|---|---|---|
| number | The number to format | Script literal or script variable | n.a. |
| format
(Optional) |
Zeros used as placeholders for the total number of digits. For floating-point numbers, specify a delimiter as the decimal point followed by zeros indicating the number of decimal places. | Script literal or script variable | 0 |
Important Considerations
-
The number of zeros in the format parameter defines the total number of digits. If the number already has at least that many digits, no leading zeros are added. Leading zeros are removed when this parameter is used.
-
If you include a plus sign (+) as the first character in the format parameter, positive numbers are displayed with an explicit plus sign (for example, "+1.00"). In this case, the target variable must have data type string. If the result is zero because all decimal places were removed, the sign is also removed. For more information, see Script Variable Data Types.
-
For floating-point numbers, the number of decimal places is controlled by the zeros after the delimiter in the format parameter. Numbers with more decimal places are truncated without rounding. If the number has fewer decimal places than specified, trailing zeros are added. If no decimal point is specified, all decimal places are removed. A formatted float value must be assigned to a variable of type string.
Examples
The following example removes leading zeros from the 16-digit return code of SYS_BUSY_60 and prints the result to the activation report.
: SET &SRV# = SYS_BUSY_60 ()
: SET &RET# = FORMAT (&SRV#)
: PRINT &RET#
The following example adds leading zeros to reach a total length of 5 digits and prints the result (00125) to the activation report.
: SET &RET# = FORMAT ("125", "00000")
: PRINT &RET#
The following example removes leading zeros and prints the result (333) to the activation report.
: SET &RET# = FORMAT ("0000333", "00")
: PRINT &RET#
In the following example, the number is unchanged because it has no leading zeros and already fits the specified format.
: SET &RET# = FORMAT ("555", "00")
: PRINT &RET#
The following example formats a floating-point number to one decimal place and prints the result (-0.7) to the activation report.
:DEFINE &NUM#,float
: DEFINE &RET#,string
: SET &NUM#=-0.75
: SET &RET# = FORMAT (&NUM#, "00.0")
: PRINT &RET#
The following example removes all decimal places and prints the result (0000) to the activation report.
:DEFINE &NUM#,float
: DEFINE &RET#,string
: SET &NUM#=0.65
: SET &RET# = FORMAT (&NUM#, "0000")
: PRINT &RET#
See also: