Aligning Numbers

See all articles from Sinc-Link v7 n2
Author(s): Mel Richardson
Publication: Sinc-Link
Pub Details: Sinc-Link v7 n2 (Volume 7, Issue 2)
Date: March/April 1989
Pages: 13

The output of any program involving lists of numbers is usually easier to read and better looking if the numbers are lined up from a common point. This is sometimes accomplished in other Basic dialects by the “PRINT USING XXXX.XXXX” function.

Doing this in ZX81 Sinclair Basic can get pretty tangled. The book “CONVERTING to T/S BASIC” by Stuart Bird uses a subroutine and becomes ponderous for short programs. An easier way, I have found, is to use the “STR$” function to convert numbers to “STRINGS” and employ the excellent string handling ability of our computers to control printing.

An example is this fragment of a program that outputs a “continued fraction” from a decimal input. Four numbers are printed;F,D1,D & R and we wish to align the fraction “D1/D” so…

240 LET N$=STR$ D1
250 PRINT F;TAB 11-LEN N$;N$;"/";D;TAB 20;R

and the output is shown below.

3.1415927

3          3/1        3
7         22/7        3.1428571
15       333/106      3.1415094
1        355/113      3.1415929
292   103993/33102    3.1415927
2     208341/66317    3.1415927

The “TAB 11” is the key which indicates the point in this example to have the numbers lined up at. -LEN N$ tells the machine where to start and the “/” will be at TAB 12.

This example demonstrates how easily strings are manipulated compared to numbers and variations of string handling and slicing should solve most situations like this.

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