Compaq BASIC for OpenVMS
Alpha and VAX Systems
User Manual


Previous Contents Index


Chapter 15
Formatting Output with the PRINT USING Statement

The PRINT USING statement controls the appearance and location of data on a line of output. With it, you can create formatted lists, tables, reports, and forms. This chapter describes how to format data with the PRINT USING statement.

15.1 Overview of the PRINT USING Statement

The ability to format data with the PRINT USING statement is useful because the way in which BASIC displays data with the PRINT statement is often limited. For example, a program might use floating-point numbers to represent dollars and cents. The PRINT statement displays floating-point numbers with up to six digits of accuracy, and places the decimal point anywhere in that 6-digit field. In contrast, PRINT USING lets you display floating-point numbers in the following ways:

Formatting monetary values in this way provides a more readable report. Another use for formatted numeric values might be to print checks on a printer. PRINT USING lets you print numbers with a dollar sign and an asterisk-filled field preceding the first digit.

PRINT USING also formats string data. With it you can left- and right-justify string expressions, or center a string expression over a specified column position. Further, the PRINT USING statement can contain string literals. These are strings that do not control the format of a print item, but instead are printed exactly as they appear in the format string.

It is recommended that you declare all format expressions as string constants. When you do this the BASIC compiler instructs the Run-Time Library to compile the string at compile time rather than at run time, thus improving the performance of your code.

15.2 Using Format Strings

Format strings determine the way in which items are to be printed in the output file. Format strings can be any of the following:

The PRINT USING statement must contain one or more format strings. Each format string is made up of one format field. Each format field controls the output of one print item and can contain only certain characters, as described throughout the chapter.

The PRINT USING statement must also contain a list of items you want printed. To format print items, you must separate them with commas or semicolons. Separators between print items do not affect output format as they do with the PRINT statement. However, if a comma or semicolon follows the last print item, BASIC does not return the cursor or print head to the beginning of the next line after it prints the last item in the list.

When BASIC encounters an invalid character within the current format field, it automatically ends the format field; therefore, you do not need to delimit format fields. The character that terminates the previous field can be either a new format field or a string literal.

In the following example, the first three characters in the format string (###) make up a valid numeric format field. The fourth character (A) is invalid in a numeric format field; therefore, BASIC ends the first format field after the third character. BASIC continues to scan the format string, searching for a character that begins a format field. The first such character is the number sign at character position 7. Therefore, the characters at positions 4, 5, and 6 are treated as a string literal. The characters at positions 7, 8, and 9 make up a second valid numeric format field.


PRINT USING "###ABC###", 123, 345 

Output


123ABC345 

When the statement executes, BASIC prints the first number in the list using the first format field, then prints the string literal ABC, and finally prints the second number in the list using the second format field. If you were to supply a third number in the list, BASIC would reuse the first format string. This is called reversion.


PRINT USING "###ABC###", 123, 345, 
564 

Output


123ABC345 
564ABC 

Because any character not part of a format field is printed just as it appears in the format field, you can use a space or multiple spaces to separate format fields in the format string as shown in the following example:


DECLARE STRING CONSTANT format_string = "###.##  ###.##" 
DECLARE SINGLE A,B 
A = 2.565 
B = 100.350 
PRINT USING format_string, A, B, A, B 

Output


  2.57  100.35 
  2.57  100.35 

When the BASIC compiler encounters the PRINT USING statement, BASIC prints the value of A (rounded according to PRINT USING rules), three spaces, then the value of B. BASIC prints the three spaces because they are treated as a string literal in the format string. Notice that when BASIC reuses a format string, it begins on a new line.

15.3 Printing Numbers

With the PRINT USING statement, you can specify:

Unlike the PRINT statement, PRINT USING does not automatically print a space before and after a number. Unless you reserve enough digit positions to contain the integer portion of the number (and a minus sign, if necessary), BASIC prints a percent sign (%) to signal this condition and displays the number in PRINT format.

15.3.1 Specifying the Number of Digits

You reserve places for digits by including a number sign (#) for each digit position. If you print negative numbers, you must also reserve a place for the minus sign.


PRINT USING "###",123      !Three places reserved 
PRINT USING "#####",12345  !Five places reserved 
PRINT USING "####",-678    !Four places reserved 
END 

Output


123 
12345 
-678 

If there are not enough digits to fill the field, BASIC prints spaces before the first digit.


format_string$ = "#####" 
PRINT USING format_string$, 1 
PRINT USING format_string$, 10 
PRINT USING format_string$, -1709 
PRINT USING format_string$, 12345 
END 

Output


     1 
    10 
 -1709 
 12345 

If you have not reserved enough digits to print the fractional part of a number, BASIC rounds the number to fit the field.


PRINT USING "###",126.7 
PRINT USING "#",5.9 
PRINT USING "#",5.4 
END 

Output


127 
6 
5 

If you have not reserved enough places to print a number's integer portion, BASIC prints a percent sign as a warning followed by the number in PRINT statement format. After BASIC prints the number, it completes the rest of the list in PRINT USING format.

In the following example, PRINT USING displays the first number. Because there are not enough places to the left of the decimal point to display a 3-digit number, BASIC prints the second number in PRINT statement format, with a space before and after, but includes a percent sign warning.


PRINT USING "###", 256 
PRINT USING "##", 256 
END 

Output


256 
% 256 

15.3.2 Specifying Decimal Point Location

The decimal point's position in the format string determines the number of reserved places on either side of it. If the print item's fractional part does not use all of the reserved places to the right of the decimal point, BASIC fills the remaining spaces with zeros.


DECLARE STRING CONSTANT FM = "##.###" 
PRINT USING FM, 15.72 
PRINT USING FM, 39.3758 
PRINT USING FM, 26 

Output


15.720 
39.376 
26.000 

If there are more fractional digits than reserved places to the right of the decimal point, BASIC rounds the number to fit the reserved places. Note that there must be enough places reserved to the left of the decimal point for the integer portion of the number. Otherwise, BASIC prints the number in PRINT format preceded by a percent sign. The following example shows how PRINT USING rounds numbers when you specify decimal point location:


PRINT USING "##.##", 25.789 
PRINT USING "##.###", 100.2 
PRINT USING "#.##",.999 
END 

Output


25.79 
% 100.2 
1.00 

BASIC fills all reserved spaces to the left of the decimal point with specified digits, spaces, or the minus sign.


PRINT USING "##.##", 5.25 
PRINT USING "##.##", -5.25 
PRINT USING "###.##,-5.25 
END 

Output


 5.25 
-5.25 
 -5.25 

15.3.3 Printing Numbers with Special Symbols

Special symbols let you print numbers with trailing minus signs, asterisk-fill fields, floating currency symbols, commas, or E notation. You can also specify debits, credits, leading zeros, leading blanks, and blank-if-zero fields. Table 15-1 summarizes these special characters.

.
Table 15-1 Format Characters for Numeric Fields
Character Effect on Format
Number sign (#) Reserves a place for one digit.
Decimal point (period)(.) Determines decimal point location and reserves a place for the radix point.
Comma (,) Prints a comma before every third digit to the left of the decimal point and reserves a place for one digit or digit separator.
Two asterisks (**) Print leading asterisks before the first digit and reserve places for two digits.
Two dollar signs ($$) Print a currency symbol before the first digit. They also reserve places for the currency symbol and one digit. By default, the currency symbol is a dollar sign. To change the currency symbol, see Section 15.3.3.3
Four carets (^^^^) Print a number in E (exponential) format and reserve four places for E notation.
Minus sign (-) Prints a trailing minus sign for negative numbers. Printing a negative number in an asterisk-fill or a currency field requires that the field also have a trailing minus sign or credit/debit character.
Zero in angle brackets (<0>) Prints leading zeros instead of leading spaces.
Percent sign in angle brackets (<%>) Prints all spaces in the field if the value of the print item, when rounded to fit the numeric field, is zero.
CD in angle brackets (<CD>) Prints credit and debit characters immediately following the number. BASIC prints CR for negative numbers and zero, and DR for positive numbers.
Underscore (_) Specifies that the next character is a literal, not a formatting character.

15.3.3.1 Commas

You can place a comma anywhere in a number field to the left of the decimal point or to the right of the field's first character. A comma cannot start a format field. BASIC prints a comma to the left of every third digit from the decimal point. If there are fewer than four digits to the left of the decimal point, BASIC omits the comma.


PRINT USING "##,###",10000 
PRINT USING "##,###",759 
PRINT USING "$$#,###.##",25694.3 
PRINT USING "**#,###",7259 
PRINT USING "####,#.##",25239 
END 

Output


 10,000 
    759 
 $25,694.30 
 **7,259 
 25,239.00 

15.3.3.2 Asterisk-Fill Fields

To print an asterisk (*) before the first digit of a number, you must start the field with two asterisks.


DECLARE STRING CONSTANT FM = "**##.##" 
PRINT USING FM, 1.2 
PRINT USING FM, 27.95 
PRINT USING FM, 107 
PRINT USING FM, 1007.5 
END 

Output


***1.20 
**27.95 
*107.00 
1007.50 

Note that the asterisks reserve two places as well as cause asterisk fill.

To specify a negative number in an asterisk-fill field, you must place a trailing minus sign in the field. The trailing minus sign must be the last character in the format string.


DECLARE STRING CONSTANT FM = "**##.##-" 
PRINT USING FM, 27.95 
PRINT USING FM, -107 
PRINT USING FM, -1007.5 
END 

Output


**27.95 
*107.00- 
1007.50- 

If you try to print a negative number in an asterisk-fill field that does not include a trailing minus sign, BASIC signals "PRINT USING format error" (ERR=116).

You cannot specify both asterisk-fill and zero-fill for the same numeric field.

15.3.3.3 Currency Symbols

To print a currency symbol before the first digit of a number, you must start the field with two dollar signs. If the data contains both positive and negative numbers, you must include a trailing minus sign.


DECLARE STRING CONSTANT FM = "$$##.##-" 
PRINT USING FM, 77.44 
PRINT USING FM, 304.55 
PRINT USING FM, 2211.42 
PRINT USING FM, -125.6 
PRINT USING FM, 127.82 
END 

Output


 $77.44 
$304.55 
% 2211.42 
$125.60- 
$127.82 

Note that the dollar signs reserve places for the currency symbol and only one digit; the dollar sign is always printed. (Hence the warning indicator (%) when the third PRINT USING statement executes.) Contrast this with the asterisk-fill field, where BASIC prints asterisks only when there are leading spaces.

By default, the currency symbol is a dollar sign. On OpenVMS systems, you can change the currency symbol, radix point, and digit separator by assigning the characters you want to the logical names SYS$CURRENCY, SYS$RADIX_POINT, and SYS$DIGIT_SEP, respectively.

If you try to print a negative number in a dollar sign field that does not include either a trailing minus sign or the CR and DR formatting character, BASIC signals "PRINT USING Format error" (ERR=116).

15.3.3.4 Negative Fields

To allow for a field containing negative values, you must place a trailing minus sign in the format field. A negative format field causes the value to be printed with a trailing minus sign. You can also denote negative fields with CR and DR. See Section 15.3.3.8 for more information.

You must use a trailing minus or the CR/DR formatting character to indicate a negative number in an asterisk-fill or floating dollar sign field.

For fields with trailing minus signs, BASIC prints a minus sign after negative numbers as shown in Example 1, and a space after positive numbers as shown in Example 2.

Example 1


!Standard field 
PRINT USING "###.##",-10.54 
PRINT USING "###.##",10.54 
END 

Output 1


-10.54 
 10.54 

Example 2


!Fields with Trailing Minus Signs 
PRINT USING "##.##-",-10.54 
PRINT USING "##.##-",10.54 
END 

Output 2


10.54- 
10.54 

15.3.3.5 E (Exponential) Format

To print a number in E format, you must place four carets (^^^^) at the end of the field. The carets reserve space for:

In exponential format, BASIC does not pad the digits to the left of the decimal point. Instead, the most significant digit shifts to the leftmost place of the format field, and the exponent compensates for this adjustment.


PRINT USING "###.##^^^^",5 
PRINT USING "###.##^^^^",1000 
PRINT USING ".##^^^^",5 
END 

Output


 500.00E-02 
 100.00E+01 
.50E+01 

If you use fewer than four carets, the number does not print in E format; the carets print as literal characters. If you use more than four carets, BASIC prints the number in E format and includes the extra carets as a string literal.


PRINT USING "###.##^^^",5 
PRINT USING "###.##^^^^^",5 
END 

Output


   5.00^^^ 
 500.00E-02^ 

You must reserve a place for a minus sign to the left of the decimal point to display negative numbers in exponential format. If you do not, BASIC prints a percent sign (%) as a warning.

You cannot use exponential format with asterisk-fill, floating-dollar sign, or trailing minus formats.

15.3.3.6 Leading Zeros

To print leading zeros in a numeric field, you must start the format field with a zero enclosed in angle brackets (<0>). These characters also reserve one place for a digit.


DECLARE STRING CONSTANT FM = "<0>####.##" 
PRINT USING FM, 1.23, 12.34, 123.45, 1234.56, 12345.67 

Output


00001.23 
00012.34 
00123.45 
01234.56 
12345.67 

When you specify zero-fill, you cannot specify asterisk-fill or floating-dollar sign format for the same field.

15.3.3.7 Blank-If-Zero Fields

To print a blank field for values which round to zero, you must start the numeric field with a percent sign enclosed in angle brackets (<%>).

In the following example, PRINT USING displays spaces in each reserved position for the second and third items in the list. The value of the second item is zero, while the value of the third item becomes zero when rounded to fit the numeric field.


DECLARE STRING CONSTANT FM = "<%>####.##" 
PRINT USING FM, 1000, 0, .001, -5000 

Output


  1000.00 
 
 
 -5000.00 


Previous Next Contents Index