ZX81: Number Formatting Print Using

See all articles from Sinc-Link v9 n4
Publication: Sinc-Link
Pub Details: Sinc-Link v9 n4 (Volume 9, Issue 4)
Date: July/August 1991
Pages: 26

Most print formatting problems can be solved using a combination of truncating, rounding, or aligning the decimal point. However other versions of Basic have a very powerful statement PRINT USING that allows a wide range of number formats to be specified. It is possible to write a sub-routine that provides some of the capabilities of the PRINT USING command and this would be useful both for converting programs and for writing new programs.

The format of a number produced by a PRINT USING statement is specified by the use of a “picture” of the nember stored in a string. For example, in most Basics “###.##” would specify a format of three spaces or digits in front of the decimal point and two digits following, i.e. 3.123 printed using that format would be blank|blank|3|.|1|2. There are many other foramatting symbols that can be combined with # to form a “picture” of the number, but perhaps the most useful is the “floating” money sign. If you write either a dollar or a pound sign in front of the formatting “picture”, the money sign will be printed to the immediate left of the formatted number.. For example,”###.###” would format 3.1234 as blank|blank|3|.|1|2|3. This method af drawing a “picture” of the number is a very easy to use and powerful formatting method. For example, if you don’t want a decimal point printed then all you have to do is leave it out of the “picture”, i.e. “###”. If the number to be printed it too big for the space allocated to it by the “picture” then it is printed unformatted.

A general PRINT USING routine for the ZX81 would be rather long, but we can produce a subroutine that will accept a “picture” involving digit positions marked by an #, the decimal point and floating money signs. The only change that we have to make to the usual PRINT USING is to change the # to * because the ZX81 doesn’t have a # character. The PRINT USING subroutine and a small test program is:

  10 LET U$="$*******.**"
20 PRINT TAB 9;U$
30 INPUT V
40 GOSUB 2000
50 LET V=V*100*RND
60 PRINT
70 GOTO 20

2000 LET H$=STR$ INT V
2010 LET L$=(STR$(V))(LEN H$+1 TO)
2020 IF L$<> "" THEN IF L$(1)="." THEN LET L$=L$(2 TO)
2030 LET S$=U$(1)
2040 IF U$(1) <> " " AND U$(1) <> "$" THEN LET S$=""
2050 LET F=0
2060 LET M=0
2070 LET N=0
2080 FOR I=1 TO LEN U$
2090 IF U$(I)="." THEN LET F=1
2100 IF U$(I)="*" AND F=0 THEN LET M=M+1
2110 IF U$(I)="*" AND F=1 THEN LET N=N+1
2120 NEXT I
2130 IF M=0 AND H$="0" THEN LET H$=""
2140 LET H$=S$+H$
2150 IF LEN H$>M THEN GOTO 2170
2160 LET H$=" "(1 TO M-LEN H$+H$
2170 IF F <> 0 THEN LET H$=H$+"."
2180 LET L$=L$+"000000000000"
2190 IF N <> 0 THEN LET H$=H$+L$(1 TO N)
2200 PRINT H$;
2210 RETURN

The “picture” is stored in the string U$ at line 10. Lines 20-70 simply send test values in V for subroutine 2000 to format. Before any formatting begins the number contained in V is converted into a string by STR$ and then split into two parts. The digits in front of the decimal point are stored in H$ by line 2000 and the digits following the decimal point are stored in L$ by lines 2010-2020. Notice the use of IF…THEN, IF…THEN construction in line 2020. This has the same effect as IF….AND…THEN, but is needed because in this case the second condition, i.e. L$(1)=”.”, can only be worked out if the first condition is true i.e. if L$ <> “”. The expression IF L$<>” ” AND L$(1) THEN… will give an error message if L$ is null because in this case L$(1) doesn’t exist. The variable S$ is used to hold any floating money sign in the formatting string U$; if there is no such sign then S$ is set to the null string (Lines 2030-2040). Lines 2050-2120 count the number of digits before the decimal point (M) and the number of digits after the decimal point (N) in the formatting string U$. The variable S is zero if no decimal point is found. Line 2020 removes the leading zero if the number is less than one and there is no digit position specified by the format. Line 2030 adds the money sign, if any, to the front of the number. The “padding” blanks are then added to the number by lines 2150 and 2160. If a decimal point is required it is added in line 2170 and then the fractional part stored in L$ is padded with trailing zeros (line 2180) before being truncated to fit into the correct number of digits by line 2190. Finally, line 2200 prints the fully formatted number.

Three samples illustrating different formats:

.****
.0001
.0086
.7543
30.9278
2318.9732
54588.0130
3579173.1000
62866000.0000

*.****
0.0001
0.0006
0.0058
0.2472
23.0431
2067.3304
59288.7480
3023603.1000

 ********.**
        0.01
        0.43
       18.18
      993.24
    96608.67
  9170571.30

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