This handy program incorporates several features, tabulating numbers to a desired number of places to the right of the decimal point, and tabulating so the decimal point is in a fixed column to makes a nice looking display.
A few comments about the program–in place of the usual method of calculating the rounding (INT(v*100+.5)/100) for 2 places, lines 220 and 230 make the number of places variable so the user can input the desired number. Lines 330 and 350 display in the first column the number to be rounded while the second column displays the rounded numbers in justified form.
In operation the second column starts the TAB from a fixed point minus the length of the number, this makes it right justify. In order to use LEN N$, the calculated rounded number is converted to a string in line 300 so the length of the number can be used for Tab.
If a variable number of decimal places is required as an input, just change the GO TO in lines 330 and 350 to 200.
If a hard copy of the numbers is desired change the PRINTs in lines 330 and 350 to LPRINTs, or use COPY after the table is completed. Column headings can be added if desired.
This is a program useable on a 2068, TS 1000, or other BASIC language. If other than Sinclair some changes will probably be required.
10 PRINT " ******************"
20 PRINT " *ROUNDING NUMBERS*"
30 PRINT " ******************"
40 REM DICK WAGNER 7/93
100 REM N=NUMBER TO BE ROUNDED
110 REM DP=DIGITS TO THE RIGHT OF DECIMAL POINT
120 REM R=VALUE ROUNDED
200 PRINT ;"HOW MANY PLACES TO RIGHT OF DECIMAL POINT FOR ROUNDING? ";: INPUT DP: CLS
205 PRINT "INPUT NUMBER TO BE ROUNDED": PRINT
210 INPUT N;
220 LET A=N+5*10^-(DP+1)
230 LET R=INT (A*10^DP)/10^DP
300 LET N$=STR$ R
310 FOR L=1 TO LEN N$
320 IF N$(L)="." THEN GO TO 350
325 NEXT L
330 PRINT N;: PRINT TAB (20-LEN N$);N$: GO TO 210
350 PRINT N;: PRINT TAB (21-L);N$: GO TO 210
Transcription note: the listing is corrected. As printed, line 220 reads LET A=N+5.5*10^-(DP+1), which rounds some numbers up wrongly (1.0047 to two places gave 1.01); corrected to 5. The sample output is unchanged.
INPUT NUMBER TO BE ROUNDED .009999 .01 0.9945 0.995 10.0009 10.001 900012 900012 55.34555 55.346
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.