Often a program will require a column of numbers to be printed. The ZX-81 normally will not align decimal points and this leaves a ragged appearance. The following subroutine will place a column of figures with their decimal points aligned:
9000 PRINT "(7 or so spaces)"(1 TO M-(V > 1) * INT (LN
V/LN 10) + (V <.1)); V
9010 RETURN
V = number to be printed
M = sets field width i.e. number of printing positions before the decimal point
M is set earlier in the program. e.g. LET M = 7
To demonstrate the subroutine set-up, this is the following program:
10 LET M=7
20 INPUT V
30 GOSUB 9000
40 GOTO 20
9000 (as shown earlier)
9010 RETURN
Essentially, line 9000 determines the difference between the number of characters to the left of the decimal in V; subtracts it from M; and adds spaces (sliced from the blank string in line 9000) to V before printing V.
The numbers of spaces in line 9000 should not be less than the value of M.
DECIMAL CONTROL
THE ZX-81 has rather primitive number handling facilities when it comes to printing. If you ask it to print a computed value, you will have no control over the number of figures which will be printed after the decimal point. This can be troublesome especially if it is a money figure you wish to display.
One method of handling this is by means of the following subroutine:
9000 LET A = 10 ** N
9010 PRINT INT (V * A)/A
Where:
V = the number to be printed
N = the number of digits to follow the decimal point.
Enter the following demonstration program:
10 LET N=2
20 INPUT A$
30 LET V=VAL A$
40 GOSUB 9000
50 GOTO 10
9000 as shown
9010 as shown
9000 RETURN
Run and enter the decimal values per line 20. Note that on line 9000 the “**” is found on Key ‘H’ (meaning: to the power of..). Line 20 is used to convert the variable to be treated to the variable ‘V’ used in the subroutine.
As it stands, line 9010 chops off all unwanted numbers; i.e. it rounds DOWN. If you want the program to round to the nearest number then make line 9010 as follows:
9010 PRINT INT (V * A + .5)/ A
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.