Your ZX/TS prints out data to many decimal places, most of them extraneous. The number’s awkward length and changeable format make it hard to design a neat, compact output display. To fix this, you can format data in several ways before you print it out.
Round/Truncate: To truncate any number A to n decimal places and round the last digit,
LET A=INT((A+.5/1En)*1En)/1En
before printing. Substitute the number of decimal places you want for n in this statement (for example, 1E3 for 3 decimal places).
Exponential Format: Your ZX/TS doesn’t revert to exponential notation unless a number is greater than 10¹³ or less than 10⁻⁵. In between, you spend a lot of time tapping the screen with a pencil to count digits. Sending A to the following subroutine produces a string, A$, that is truncated and rounded to n significant figures, and formatted to a predictable length of n+7 characters. (For example: for n=3, -17.566019 goes to -0.176E+02.) Insert a value for n in place of 3 in line 1070 and LEN A$>n+3 in line 1130.
Fixed Point, Commas: Multibillionaires like to see their dollars spelled out, with commas between every 3 digits. They also like numbers stacked in columns with the decimal points and commas aligned. The following rather bulky subroutine accomplishes this. First, enter LET N1=… early in the calling program (or in the immediate mode), defining N1 to be the column number where decimal points fall. The subroutine converts any number A into the string A$ (for example, 9,422), and specifies N2, the column number for the first character of A$. PRINT AT XX,N2;A$ statements align decimals and commas of successive output numbers.
This subroutine works for all numbers except negative exponents. Presumably you wouldn’t use this notation for small numbers. You could incorporate the decimal truncation statement given in paragraph two into line 1030 of this subroutine to eliminate, for example, fractions of a cent.
1000 REM --EXPONENTIAL FORMAT---
1010 IF A<>0 THEN GOTO 1050
1020 LET C=0
1030 LET E=0
1040 GOTO 1080
1050 LET B=LN ABS A/2.3025851
1060 LET C=INT B+1
1070 LET E=INT ((10**(B-C)+.5/1E3)*1E3)/1E3
1080 LET F$=STR$ ABS C
1090 IF LEN F$=1 THEN LET F$="0"+F$
1100 LET G$="+"
1110 IF SGN C=-1 THEN LET G$="-"
1120 LET A$=STR$ E
1130 IF LEN A$>=5 THEN GOTO 1160
1140 LET A$=A$+" "
1150 GOTO 1130
1160 LET A$=A$+"E"+G$+F$
1170 IF A>=0 THEN GOTO 1200
1180 LET A$="-"+A$
1190 RETURN
1200 LET A$=" "+A$
1210 RETURN
SYNTACTIC SUM: 27625, 8K/1K
1000 REM --FIXED POINT, COMMAS--
1010 LET A$=""
1020 LET E$=""
1030 LET B=ABS A
1040 IF B-INT B=0 OR B>1E13 THEN GOTO 1110
1050 IF B>=0.1 THEN GOTO 1090
1060 LET E$=STR$ B
1070 LET A$="0"
1080 GOTO 1230
1090 LET C$=STR$ B
1100 LET E$=C$(LEN STR$ INT B+1 TO LEN C$)
1110 LET C$=STR$ INT B
1120 IF B<1E13 THEN GOTO 1160
1130 LET A$=",000"+A$
1140 LET B=B/1000
1150 GOTO 1110
1160 LET I=LEN C$-2
1170 IF I<1 THEN LET I=1
1180 IF I=1 THEN GOTO 1220
1190 LET A$=","+C$(I TO LEN C$)+A$
1200 LET C$=C$(1 TO LEN C$-3)
1210 GOTO 1160
1220 LET A$=C$+A$
1230 IF A>=0 THEN GOTO 1250
1240 LET A$="-"+A$
1250 LET N2=N1-LEN A$+1
1260 LET A$=A$+E$
1270 IF N2>=0 THEN RETURN
1280 LET N2=N2+32
1290 GOTO 1270
SYNTACTIC SUM: 38662, 8K/1K
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.