Tidying Up Your Display

See all articles from SYNC v3 n1
Author(s): Nick Godwin
Publication: SYNC
Pub Details: SYNC v3 n1 (Volume 3, Issue 1)
Date: January/February 1983
Pages: 38

A list of numbers is conventionally written down aligned on the right. This poses a problem on the ZX81, which lines numbers up the left.

Handwritten     ZX81 display
    96              96
   920              920
     4              4
  ----              ----
  1020              1020

A routine to print, display, and summate a series of values may look as follows:

 110 LET T=0
120 FOR J=1 TO 10
130 INPUT A
140 LET T=T+A
150 PRINT A
160 NEXT J
170 PRINT
180 PRINT T
999 STOP

The problem is, how to change the ZX81 display to conform to normal convention—how to align the values to the right.

The following amendment to the above does the trick, for integers (positive or negative) only:

 110 LET T=0
120 FOR J=1 TO 10
130 INPUT A
140 LET T=T+A
145 LET X$=STR$ A
150 GOSUB 1000
160 NEXT J
170 PRINT
175 LET X$=STR$ T
180 GOSUB 1000
999 STOP
1010 PRINT TAB 17-LEN X$;X$
1020 RETURN

The routine prints the values neatly in the middle of the screen. However, it is very likely that you may want to print out decimal values in a similar format. The following subroutine (replacing the one above at line 1010) does the trick:

1010 IF X$(1)="." THEN LET X$="0"+X$
1020 IF LEN X$>1 THEN IF X$(1 TO 2)="-." THEN LET X$="-0"+X$(2 TO )
1030 FOR K=1 TO LEN X$
1040 IF X$(K)="." THEN GOTO 1070
1050 NEXT K
1060 LET X$=X$+".0"
1070 PRINT TAB 17-K;X$
1080 RETURN

To test this routine, RUN the program and enter a series of wildly different values, e.g.:

46
9.009
-34456789.98765
-0.0000002
.0000034
8.6
88888.97
9999999
.01
.09

In practice, you will be fairly unlikely to require such a wide range of values for addition purposes, but the routine works equally well for any kind of arithmetical manipulation, or for any other list of numbers.

It may well be that you require the program to add sums of money, in which case you will want displayed only the value to two decimal places (representing pence or cents). To achieve this, the routine can be amended by adding or changing the following lines:

 110 LET T=0
120 FOR J=1 TO 10
130 INPUT A
140 LET T=T+A
145 LET X=A
150 GOSUB 1000
160 NEXT J
170 PRINT
175 LET X=T
180 GOSUB 1000
999 STOP
1000 LET X$=STR$ (INT (X*100+.5)/100)
1010 IF X$(1)="." THEN LET X$="0"+X$
1020 IF LEN X$>1 THEN IF X$(1 TO 2)="-." THEN LET X$="-0"+X$(2 TO )
1030 FOR K=1 TO LEN X$
1040 IF X$(K)="." THEN GOTO 1070
1050 NEXT K
1060 LET X$=X$+".00"
1070 IF K=LEN X$-1 THEN LET X$=X$+"0"
1080 PRINT TAB 17-K;X$
1090 RETURN

This routine prints the values neatly displayed in the middle of the screen, with the decimal points at TAB 16. The display routine does not affect the arithmetical calculation: you can enter as many digits as you like after the decimal point, and for the display they will be rounded to the first two decimal places. The lateral position of the display can be altered by adjusting the number (17) in line 1080, but care must be taken to allow for extra long numbers. The bug is that a value of .005 (half a pence or cent) prints as 0.00, not very serious, and probably not worth the bytes required to correct it.

The program as written occupies 394 bytes of RAM, 486 including the variables, and approximately 890 bytes including the display. Thus it is operational on 1K. The subroutine alone occupies 268 bytes.

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.