SUBROUTINE TO ALIGN DECIMAL POINTS (ZX81)
Subroutine:
3000 PRINT " "(1 to M-(V>1)*INT(LN V/LN 10)+(V<.1));V
3010 RETURN
Demonstration program:
10 LET M = 7
20 INPUT V
30 PRINT TAB 10
40 GOSUB 3000
50 LET V = Y*10 * RND
60 GOTO 30
where:
V = Number to be printed
M = sets field width i.e. No of printing positions before the decimal point
N = Number of digits before the decimal point
The number of spaces put into line 3000 needs to be sufficient to handle the longest number; i.e. on the left side of the decimal point.
SUBROUTINE TO LIMIT THE NUMBER OF DIGITS PRINTED AFTER THE DECIMAL POINT (ZX81)
The subroutine:
1000 LET DIG = 10**N
1010 PRINT INT(V*DIG)/DIG;
1020 RETURN
A demonstration program:
10 FOR N = 1 TO 10
20 LET V = RND
30 PRINT "DIGITS =";N;TAB 15;
40 GOSUB 1000
50 PRINT
60 NEXT N
70 STOP
Where:
V = number to be printed
N = max. no. of digits to follow decimal point
The demo program simply generates a series of random numbers by the subroutine
This subroutine truncates the number. If you are happier with “rounding” the number then convert LINE 1010 to read:
1010 PRINT INT(V* DIG+.5)/DIG
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.