How to print numbers as columns in Commodore BASIC

Published: April 2, 2018, 4:10 a.m.


\nIn this episode I’m demonstrating how to print numbers in evenly spaced columns in Commodore BASIC.
\nOn the C128 and the Plus/4 we can use a nifty little function called PRINT USING for this, with which we can format the output of any printed text or variable.
\nOn the C64 and VIC-20 that function doesn’t exist, so we’ll have to convert a numeric value into a string (using the STR$ function), and then determine how long our string is. Following that we’ll have to manually pad our string value with as many spaces as are required.
\nBASIC 3.5 / BASIC 7
\nHere’s the full code for the C128 and Plus/4, which is based on my earlier Lottery Number generator listing. PRINT USING appears in line 120:
\n10 x=rnd(-ti)
\n20 for i=1 to 6
\n30 rn=int(rnd(1)*49)+1
\n40 for j=1 to i
\n50 if n(j)=rn then 30
\n60 next j
\n70 n(i)=rn
\n80 next i
\n100 print:gosub 200
\n110 for i=1 to 6
\n120 print using "#####";n(i);
\n130 next
\n140 print
\n199 goto 20
\n200 rem bubble sort
\n210 for i=5 to 1 step -1
\n220 for j=1 to i
\n230 x=n(j):y=n(j+1)
\n240 if x>y then n(j)=y:n(j+1)=x
\n250 next:next
\n299 return
\nBASIC 2.0
\nAnd here’s the same listing without PRINT USING for the C64 and VIC-20. The string formatting happens in the subroutine in line 300:
\n10 x=rnd(-ti)
\n20 for i=1 to 6
\n30 rn=int(rnd(1)*49)+1
\n40 for j=1 to i
\n50 if n(j)=rn then 30
\n60 next j
\n70 n(i)=rn
\n80 next i
\n100 print:gosub 200
\n110 for i=1 to 6
\n120 gosub 300:print a$;
\n130 next
\n140 print
\n199 goto 20
\n200 rem bubble sort
\n210 for i=5 to 1 step -1
\n220 for j=1 to i
\n230 x=n(j):y=n(j+1)
\n240 if x>y then n(j)=y:n(j+1)=x
\n250 next:next
\n299 return
\n300 rem space the numbers
\n310 a$=str$(n(i))
\n320 if len(a$)=2 then a$=" "+str$(n(i))
\n399 return
\nAny questions, please let me know. Happy retro hacking!
\nPS: It got a bit dark there at the end on my webcam. Help me get some lights by supporting me on Patreon \U0001f609