07.05.2015 Views

Download - True BASIC

Download - True BASIC

Download - True BASIC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Output Statements<br />

19<br />

This section illustrates some of the more common uses of the PRINT USING statement. Appendix D contains a<br />

more formal discussion of the details.<br />

Numeric Format Fields<br />

First consider a simple example of formatting numeric values:<br />

PRINT USING “-###.###”: -Pi, 9^3<br />

The format string “-###.###” causes the PRINT USING statement to print a numeric value with exactly three<br />

decimal places, adding zeroes or rounding if necessary. It allows up to three places before the decimal point and<br />

reserves a space for the sign if the value is negative. Leading zeroes will not be printed. Thus, the output for the<br />

above statement would appear as:<br />

- 3.142 729.000<br />

You may define the format string within the PRINT USING statement, or you may define a string variable for<br />

use with PRINT USING. The following two statements produce the same output as the example above:<br />

LET format$ = “-###.###”<br />

PRINT USING format$: -Pi, 9^3<br />

In these examples, the format string contains only one field. A field is the format specification for a single print<br />

item. Since there is only one field but two print items, the field is used twice — once for each print item. Notice that<br />

each item uses eight spaces with no extra spaces separating the two print items. <strong>True</strong> <strong>BASIC</strong> follows the format<br />

string precisely and adds no additional spaces regardless of data type or punctuation between print items. If you<br />

want spaces to precede or follow each number you must include those spaces within the format string:<br />

LET format$ = “ -###.### “<br />

PRINT USING format$: -Pi, 9^3<br />

The field length is the number of character positions that the value will occupy when printed. In the format string<br />

“-###.###”, the field length is eight spaces, which includes the spaces reserved for the sign and the decimal point.<br />

Numeric values are aligned with the decimal within a field (that is, they are printed decimal justified). This means<br />

that if several numbers are printed with the same format string, one under the other, any decimal points (or commas)<br />

will line up. If no decimal point is present in the field, numeric values are aligned with the right edge of the<br />

field (right justified).<br />

In the format string “-###.###”, the first position in the field is reserved for a negative sign; it may not be occupied<br />

by a digit. Here are some examples; notice the last one in particular.<br />

Examples of Output Formatted with the String: “-###.###”<br />

——————————————————————————————————————<br />

Number<br />

Output<br />

17 17.000<br />

17.1234 17.123<br />

-123.4687 -123.469<br />

-17.2 - 17.200<br />

12345 ********<br />

——————————————————————————————————————<br />

If a number cannot be printed in the specified format, asterisks are printed instead, as in the last example. The<br />

field’s length determines the number of asterisks printed.<br />

Format fields are composed of place holders. Place holders are characters that reserve space within the field for a<br />

specific character or range of characters that may appear in the printed value. The format string “-###.###” contains<br />

three different place holders: the minus sign (–), the pound sign (#), and the decimal point (.).<br />

There are other place holders that you may use to compose fields in format strings. The following table summarizes<br />

all the place-holder characters valid for numeric values:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!