Applesoft: A program to simulate PRINT USING

The listing below demonstrates how Applesoft can simulate the BASIC statement PRINT USING.
This particular example shows a subroutine that formats numeric output into a "Dollars and Cents" format. It's simple and fairly fast. It assumes that the calling program has initialized the following:

X, containing the number to be printed,
N, containing the number of digits to the right of the decimal, and
S, containing the width of the right justified printing field

The subroutine starting at line 1000 does all the work.

100 REM PRINT USING SIMULATOR
130 REM
140 LET N = 2: REM SET NUMBER OF DECIMALS
160 LET S = 5: REM SET FIELD WIDTH
180 HOME
190 FOR X = - 5 TO 5 STEP .501
200 PRINT X,"$";
210 GOSUB 1000
220 NEXT X
230 PRINT
240 PRINT "UNFORMATTED FORMATTED"
250 END
1000 LET X$ = " " + STR$(INT(X * 10^N +.5))
1010 LET Q = LEN(X$) - (VAL(X$) < 0)
1020 PRINT SPC(S - Q * (Q > N+1) - (N+2) * (Q <= N+1));
1030 PRINT MID$ (X$,1 + (VAL(X$) < 0), (Q <= N) + (Q-N) * (Q > N));
1040 PRINT MID$ ("0.00", 1 + ((N+1) < Q), 1 + (N-Q+2) * (Q < N+2));
1050 PRINT RIGHT$ (X$, N * (Q > N) + (Q-1) * (Q <= N));
1060 RETURN
Published Date: Feb 18, 2012