Beginning at line 1000, the subroutine in this sample program formats the numerals of the string variable X$ before line 140 prints it. To specify the format, line 1020 assigns a decimal value to the numeric variable F. The value 8.2 specifies a format of 2 decimal places and 8 total characters (digits, decimal point, sign, and leading spaces). The subroutine rounds the number in X to the desired number of decimal places indicated by the number to the right of the decimal point in F. Then the subroutine pads X$ with spaces until the string is the length indicated by the number to the left of the decimal point.
100 HOME: PRINT "NORMAL","FORMATTED"
110 FOR I = 1 TO 5
120 READ X: PRINT X,
130 GOSUB 1000
140 PRINT X$
150 NEXT I
200 DATA 1,-3,.2,17,450
300 END
1000 REM PRINT FORMATTING
1010 REM
1020 F = 8.2: REM FORMAT CONTROL
1030 TC = INT (F): REM TOTAL CHARACTERS
1040 DP = VAL ( RIGHT$ ( STR$ (F),1)): REM DECIMAL PLACES
1050 X = INT (10 ^ DP * X + .5) / (10 ^ DP)
1060 X$ = STR$ (X)
1070 FOR JJ = 1 TO LEN (X$)
1080 IF MID$ (X$,JJ,1) = "." THEN 1110: REM LOOK FOR .
1090 NEXT JJ
1100 X$ = X$ + "."
1110 IF ( LEN (X$) - JJ) < DP THEN X$ = X$ + "0": GOTO 1110
1120 IF LEN (X$) < TC THEN X$ = " " + X$: GOTO 1120
1200 RETURN