This article from Mark Schafer refers to programming in Extended BASIC.
USING can also be used in a DISPLAY USING statement.
USING represents a method by which you can get data to print in a specific format instead of the default format. The syntaxes are:
PRINT USING format:print-list DISPLAY (option-list:) USING format(:print-list)
where 'format' is a line number or a string expression.
A line number would be the line where an IMAGE statement is found which would contain the string expression which is the format. A string expression can be as simple as a string literal enclosed in quotes or a complicated expression made up variables, function calls.
What USING does
100 PRINT -56.7;109.2850 110 PRINT USING "####.# ###.####":-56.7,109.285
RUN this program and it looks like this:
-56.7 109.285 -56.7 109.2850
USING allows you to better control the spacing as well as put trailing zeroes to the right of the decimal point.
The format string
is made up of fields with optional text.
Fields are like fill-in-the-blanks. They mark the place where an unknown value will be printed. Text is printed the same way every time. Fields are made up of pound (hash) signs (#) with possibly a decimal point, a minus sign, or maybe some circumflexes (^). Circumflexes follow in a later section.
Pound or hash signs take the place of a digit or sign. So in line 110 above, -56.7 printed with the field "####.#".
-56.7 has only three characters to the left of the decimal point, so the initial character is left blank; the second one is where the minus sign went. Numbers are always aligned with the decimal point. If there isn't one, it's assumed to be at the end.
If there's a minus sign in the field, it always goes at the beginning. It indicates that you want the minus sign to appear immediately to the left of the number if it is negative. However, this is the same thing another pound sign would do, so you never really need to use a minus sign (except for a more advanced purpose to be discussed later.) In other words, "-##.##" does the same thing as "###.##".
The decimal point indicates where you want it to be. The number of pound signs you put to the left of it dictates how many digits you want to the left; the number you put on the right indicates how many decimal places you want. The decimal is always printed even if there are no pound signs after it.
Too few or too many digits
If there are fewer digits in the value to the left of the decimal point than there are in the field, spaces are used to fill it out. If there are too many, the computer will refuse to print your value and fill the field with asterisks (*). This means your value is too high and/or there aren't enough places in your field. This includes the minus sign, if any.
If there are fewer digits in the value to the right of the decimal point than there are in the field, zeroes are used to fill in the remainder. If there are too many, the computer will estimate the number to the number of places given. So .## can be used to estimate to the nearest 100th, .# to the nearest tenth, and if there are no decimal places, it will estimate to the nearest unit.
Some format variations
Below is a table of how various values will be printed with various fields. The left side represents the value; across the top are the fields.
# ## ### #.# ##.## ###.###
2 2 2 2 2.0 2.00 2.000
-13 * ** -13 *** ***** -13.000
9.671 * 10 10 9.7 9.67 9.671
125.678 * ** 126 *** ***** 125.678
-.385 * -0 -0 -.4 -.39 -.385
-3.05 * -3 -3 *** -3.05 -3.050
How it works
Generally, XB (Extended BASIC) handles format strings like this:
It keeps printing text until it comes to a field (characterized by a pound sign). Then it looks for the next value to be printed. If there is one, it prints it in the format specified; if there isn't one, it terminates there and doesn't print anything else. However, you must specify at least one value and there must be at least one field in the format.
Except, curiously, DISPLAY USING doesn't need any values. Why would anyone use DISPLAY USING without any values?
If USING reaches the end of the string, and there's more values to be printed, it goes to the next line and starts over at the beginning of the string. So it's ok if the number of fields doesn't match the number of values.
Going back to syntax, if you want to use the format "I HAVE $###.##", you have three ways of doing it:
120 IMAGE I HAVE $###.## 130 PRINT USING 120:M 140 A$="I HAVE $###.##" 150 PRINT USING A$:M 160 PRINT USING "I HAVE $###.##":M
Notice that if use the first method using IMAGE, you don't need quotes. The only time you need quotes with an IMAGE statement would be when you have leading or trailing spaces. If you're going to use it repeatedly in a program, IMAGE is the most efficient method. If you're only using it once, go with the third method. Practically the only time you need the second method would be when the format string is constructed so you may not know exactly what it looks like.
The IMAGE statement must be on a line by itself. The computer ignores it when it comes to it in a program the same a it does the DATA statement, so you can put it anywhere.
Circumflexes are used to denote scientific notation which leads us to the next section....
You may want the number to come out in scientific notation (E format) even if the number normally wouldn't. To do this you put four or five circumflexes at the end of the field. Four means you want two digits in the exponent; five means you want three. If you put less than four, they will be treated as text; if you put more than five, the first five will be used, and the rest will be treated as text.
There's a little something different about E format. It always reserves the first character for the sign, so you'll need at least two #'s in the mantissa (or precede the field with a sign). Using the same numbers as above, the table can be amended thusly:
##^^^^ ####^^^^ #.####^^^^^
2 2E+00 200E-02 .2000E+001
-13 -1E+01 -130E-01 -.1300E+002
9.671 1E+01 967E-02 .9671E+001
125.678 1E+02 126E+00 .1257E+003
-.385 -4E-01 -385E-03 -.3850E+000
-3.05 -3E+00 -305E-02 -.3050E+001
This format also estimates whenever it isn't given enough places to express the exact value or tacks on zeroes when given too many. You'll probably not need this format unless you're dealing with exceedingly low or high numbers.
USING can also be used to format text values. Any field that can be used to format a number can also be used to format text. Text is just left-justified, and all characters within a field are treated the same.
### ###.### -###^^^^
BOB BOB BOB BOB
JOHN *** JOHN JOHN
25% OFF! *** ******* 25% OFF!
In general, it makes more sense to use only #'s when constructing a text field. You may want to do something like column 2 if you're printing a table of numbers, and you want to use the same format string for the column headers as in the table.
Formatting text with numbers
Of course you can mix numeric and text values for the same format as in something like:
170 IMAGE ##### HAS $###.##. 180 PRINT USING 170:"MARY",123.4
This will print:
MARY HAS $123.40.
Note the computer recognizes the period at the end as a period and not as a decimal point since the field already has one. Even if it didn't, it wouldn't make any difference since the decimal point would be printed at the end.
Now suppose you want part of a line formatted instead of a whole line. Don't worry; you can always put a semicolon at the end of a PRINT or PRINT USING statement, so you can stay on the same line for the next PRINT. You can also use this technique if you need to print a # as text instead of a field character. However, you cannot put a comma at the end of a PRINT USING statement or the computer will think you left out a value.
Using the plus sign
It can also be used like the minus sign in a field. What it will do is float the sign in front of the number be it positive or negative.
+# +### +###.##^^^^
2 +2 +2 +200.00E-02
-13 ** -13 -130.00E-01
9.671 ** +10 +967.10E-02
125.678 ** +126 +125.68E+00
-.385 -0 -0 -385.00E-03
-3.05 -3 -3 -305.00E-02
If it's negative, you get a minus sign; if it's positive or zero, you get a plus sign. There isn't one word about this in the XB manual. This can be used to write format strings for equations, like this:
200 IMAGE ###x^2+##x+## 210 PRINT USING 200:2,7,10 220 PRINT USING 200:-15,-11,1 230 PRINT USING 200:+9,33,-14
This will print:
Variable format strings
You don't want it to print those leading spaces when there aren't enough digits to fill the field. This is where you would use a variable for the format string. You would construct it so that each field would only have as many #'s as it needed to print the number (since they're all integers, that can easily be accomplished with LEN(STR$(X))) and concatenate the necessary text and use the variable as the format string. For instance, I have a program that uses a define function like this:
240 DEF MF$(X)=" $"&RPT$("#",LEN(STR$(INT(X))))&".##"
That function generates a format string for money so that there will no spaces between the dollar sign and the amount.
This places trailing zeroes after the decimal point. However, user-defined function calls take a long time in Extended BASIC, so if you need speed or if you only need it once, put a formula like this directly where you need it.
Print Using with Printer or Files
The syntax for PRINT USING with files is wrong. The correct syntax is as follows:
PRINT [#file-number,[REC record-number,]USING format:print-list That translates specifically as something like: 250 PRINT #1,USING 200:A,B,C 260 PRINT #2,REC 15,USING 200:A*4,B*4,C*4
The file being referenced is most likely the printer, but it could be a disk file. If it is, it must be a DISPLAY format file; you will get a FILE ERROR if is INTERNAL format. Obviously line 260 isn't referring to the printer.
PRINT USING is very good for printing to a printer because the printer has so many more columns to print in. So there is a good chance that you may have a program in which a particular PRINT USING is only being used to print to the printer and never to the screen.
If this is the case, there is another solution to the concatenated field problem mentioned in the last section. You can print some unprintable character between the two fields. The computer will then recognize them as two fields but they will be together on the printout! Something like this would be typical:
270 A$="##"&CHR$(0)&"###" 280 PRINT #1,USING A$:A,B
You can also use DISPLAY USING if you need to format values somewhere else on the screen (by using the AT option) or if you want to BEEP or clear the screen before doing it (ERASE ALL).