[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP BASIC for OpenVMS
User Manual


Previous Contents Index

14.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

14.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.

14.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).

14.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 14.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

14.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:

  • The capital letter E
  • A plus or minus sign (which indicates a positive or negative exponent)
  • An exponent (the exponent is 2 digits for single, double, and s_floating, 3 digits for g_floating and t_floating, and 4 digits for h_floating and x_floating)

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.

14.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.

14.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

14.3.3.8 Debits and Credits

You can have BASIC use credit and debit notation to differentiate positive and negative numbers. To do this, you place the characters <CD> (Credit/Debit) at the end of the numeric format string. This causes BASIC to print CR (Credit Record) after negative numbers, and DR (Debit Record) after positive numbers and zero.


DECLARE STRING CONSTANT FM = "$$####.##<cd>"
PRINT USING FM, -552.35, 200, -5

Output


 $552.35CR
 $200.00DR
   $5.00CR

You cannot use a trailing minus sign and Credit/Debit formatting in the same numeric field. Using the Credit/Debit formatting character causes the value to be printed with a leading space.

14.4 Printing Strings

With the PRINT USING statement, you can specify the following aspects of string format:

  • The number of characters
  • Left-justified format
  • Right-justified format
  • Centered format
  • Extended field format

Table 14-2 summarizes the format characters and their effects.

Table 14-2 Format Characters for String Fields
Character Effect on Format
Single quotation mark (') Starts the string field and reserves a place for one character.
L (upper- or lowercase) Left-justifies the string and reserves a place for one character.
R (upper- or lowercase) Right-justifies the string and reserves a place for one character.
C (upper- or lowercase) Centers the string in the field and reserves a place for one character.
E (upper- or lowercase) Left-justifies the string; expands the field, as necessary, to print the entire string; and reserves a place for one character.
Two backslashes (\ \) Reserves n+2 character positions, where n is the number of spaces between the two backslashes. PRINT USING left-justifies the string in this field. This formatting character is included for compatibility with BASIC-PLUS. It is recommended that you not use this type of field for new program development.
Exclamation point (!) Creates a 1-character field. The exclamation point both starts and ends the field. This formatting character is included for compatibility with BASIC-PLUS. It is recommended that you not use this type of field for new program development. Instead, use a single quotation mark to create a 1-character field.

You must start string format fields with a single quotation mark (') that reserves a space in the print field, followed by:

  • A contiguous series of upper- or lowercase Ls for left-justified output
  • A contiguous series of upper- or lowercase Rs for right-justified output
  • A contiguous series of upper- or lowercase Cs for centered output
  • A contiguous series of upper- or lowercase Es for extended field output

BASIC ignores the overflow of strings larger than the string format field except for extended fields. For extended fields, BASIC extends the field to print the entire string. If a string to be printed is shorter than the format field, BASIC pads the string field with spaces. For more information about extended fields, see Section 14.4.4.

A string field containing only a single quotation mark is a 1-character string field. BASIC prints the first character of the string expression corresponding to a 1-character string field and ignores all following characters.


PRINT USING "'","ABCDE"
END

Output


A

See Section 14.4.4 for an example of different types of fields used together.

14.4.1 Left-Justified Format

BASIC prints strings in a left-justified field starting with the leftmost character. BASIC pads shorter strings with spaces and truncates longer strings on the right to fit the field.

A left-justified field contains a single quotation mark followed by a series of Ls.


PRINT USING "'LLLLLL","ABCDE"
PRINT USING "'LLLL","ABC"
PRINT USING "'LLLLL","12345678"
END

Output


ABCDE
ABC
123456

14.4.2 Right-Justified Format

BASIC prints strings in a right-justified field starting with the rightmost character. BASIC pads the left side of shorter strings with spaces. If a string is longer than the field, BASIC left-justifies and truncates the right side of the string.

A right-justified field contains a single quotation mark (') followed by a series of Rs.


DECLARE STRING CONSTANT right_justify = "'RRRRR"
PRINT USING right_justify,"ABCD"
PRINT USING right_justify,"A"
PRINT USING right_justify,"STUVWXYZ"
END

Output


  ABCD
     A
STUVWX

14.4.3 Centered Fields

BASIC prints strings in a centered field by aligning the center of the string with the center of the field. If BASIC cannot exactly center the string---as is the case for a 2-character string in a 5-character field, for example---BASIC prints the string one character off center to the left.

A centered field contains a single quotation mark followed by a series of Cs.


DECLARE STRING CONSTANT center = "'CCCC"
PRINT USING center, "A"
PRINT USING center, "AB"
PRINT USING center, "ABC"
PRINT USING center, "ABCD"
PRINT USING center, "ABCDE"
END

Output


   A
  AB
  ABC
 ABCD
 ABCDE

If there are more characters than places in the field, BASIC left-justifies and truncates the string on the right.

14.4.4 Extended Fields

An extended field contains a single quotation mark followed by one or more Es. The extended field is the only field that automatically prints the entire string. In addition:

  • If the string is smaller than the format field, BASIC left-justifies the string as in a left-justified field.
  • If the string is longer than the format field, BASIC extends the field and prints the entire string.


PRINT USING "'E", "THE QUICK BROWN"
PRINT USING "'EEEEEEE', "FOX"
END

Output


THE QUICK BROWN
FOX

The following example uses left-justified, right-justified, centered, and extended fields:


PRINT USING "'LLLLLLLLL","THIS TEXT"
PRINT USING "'LLLLLLLLLLLLLL","SHOULD PRINT"
PRINT USING "'LLLLLLLLLLLLLL",'AT LEFT MARGIN'
PRINT USING "'RRRR","1,2,3,4"
PRINT USING "'RRRR",'1,2,3'
PRINT USING "'RRRR',"1,2"
PRINT USING "'RRRR","1"
PRINT USING "'CCCCCCCCC","A"
PRINT USING "'CCCCCCCCC","ABC"
PRINT USING "'CCCCCCCCC","ABCDE"
PRINT USING "'CCCCCCCCC","ABCDEFG"
PRINT USING "'CCCCCCCCC","ABCDEFGHI"
PRINT USING "'LLLLLLLLLLLLLLLLL',"YOU ONLY SEE PART OF THIS"
PRINT USING "'E","YOU CAN SEE ALL OF THE LINE WHEN IT IS EXTENDED"
END

Output


THIS TEXT
SHOULD PRINT
AT LEFT MARGIN
1,2,3
1,2,3
  1,2
    1
    A
   ABC
  ABCDE
 ABCDEFG
ABCDEFGHI
YOU ONLY SEE PART
YOU CAN SEE ALL OF THE LINE WHEN IT IS EXTENDED

14.5 PRINT USING Statement Error Conditions

There are two types of PRINT USING error conditions: fatal and warning. BASIC signals a fatal error if:

  • The format string is not a valid string expression
  • There are no valid fields in the format string
  • You specify a string for a numeric field
  • You specify a number for a string field
  • You separate the items to be printed with characters other than commas or semicolons
  • A format field contains an invalid combination of characters
  • You print a negative number in a floating-dollar sign or asterisk-fill field without a trailing minus sign

BASIC issues a warning if a number does not fit in the field. If a number is larger than the field allows, BASIC prints a percent sign (%) followed by the number in the standard PRINT format and continues execution.

If a string is larger than any field other than an extended field, BASIC truncates the string and does not print the excess characters.

If a field contains an invalid combination of characters, BASIC does not recognize the first invalid character or any character to its right as part of the field. These characters might form another valid field or be considered text. If the invalid characters form a new valid field, a fatal error condition might arise if the item to be printed does not match the field.

The following examples demonstrate invalid character combinations in numeric fields:

Example 1


PRINT USING "$$**##.##",5.41,16.30

The dollar signs form a complete field and the rest forms a second valid field. The first number (5.41) is formatted by the first valid field ($$). It prints as "$5". The second number (16.30) is formatted by the second field (**##.##) and prints as "**16.30".

Output 1


$5**16.30

Example 2


PRINT USING "##.#^^^",5.43E09

Because the field has only three carets instead of four, BASIC prints a percent sign and the number, followed by three carets.

Output 2


% .543E+10^^^

Example 3


PRINT USING "'LLEEE","VWXYZ"

You cannot combine two letters in one field. BASIC interprets EEE as a string literal.

Output 3


VWXEEE


Previous Next Contents Index