Number names: Difference between revisions

Didn't like the adaptation of the Applesoft example. Rewrote a new example processing the number input as a string.
(Added Commodore BASIC)
(Didn't like the adaptation of the Applesoft example. Rewrote a new example processing the number input as a string.)
Line 1,423:
=={{header|Commodore BASIC}}==
 
After adapting the Applesoft BASIC version to the Commodore, it became apparent there were serious rounding issues on larger numbers. (For example, 9,876,543,210 came up as nine billion eight hundred seventy-six million five hundred forty-three thousand two hundred twelve.) Other numbers were off by hundreds.
Same as the Applesoft BASIC, except that one program line was too long for input, so it was moved into a small subroutine. Also added a word-wrap routine since 40 columns on most Commodores fills up quick.
 
This example uses string variable to process the number rather than using integer or float processes. This should work on other 8-bit machines if adapted properly for their unique features and BASIC command sets.
<lang commodorebasicv2>10 print chr$(147);:input "give me a number";n
 
20 gosub 100
'''Machine specific notes for the word wrap feature:'''
25 print
 
30 gosub 400:print r$
Set <tt>'''co'''</tt> on line 20 for the following Commodore machines:
40 end
 
100 rem number name
* VIC-20: <tt>co=21</tt>.
110 if r$="" then for i=0 to 10:read s$(i),t$(i),u$(i),v$(i):next
* Commodore 64 or Plus 4: No change.
120 if n=0 then r$="zero":return
* PET: <tt>co=peek(213)</tt>. (Will detect 40 or 80 column mode.)
130 r$="":d=10:c=100:m=1e3
* CBM-II variants: <tt>co=peek(223)</tt>. (Will detect 40 or 80 column mode.)
140 a=abs(n)
* Commodore 128: <tt>co=rwindow(2)-1</tt>. (Will detect 40 or 80 column mode.)
150 for u=0 to d
 
160 h=a-c*int(a/c)
 
170 if h>0 and h<d then r$=s$(h)+" "+r$
<lang commodorebasicv2>10 print chr$(147);chr$(14)
180 if h>9 and h<20 then r$=t$(h - d)+" "+r$
20 dim s$(11),ts$(11),t$(11),o$(11):co=39
190 if h>19 and h<c then gosub 300
30 for i=0 to 11:read s$(i),ts$(i),t$(i),o$(i):next
200 h=a-m*int(a/m)
40 print "Enter a number";:input n$
210 h=int(h/c)
45 ou$=""
220 if h then r$=s$(h)+" hundred "+r$
50 if len(n$)>36 then print:print "Too long.":print:goto 40
230 a=int(a/m)
51 print
240 if a>0 then h=a-m*int(a/m):if h then r$=v$(u)+" "+r$
55 rem check for negative
250 if a>0 then next u
26056 if left$(n<0$,1)="-" then rn$=right$(n$,len(n$)-1):ou$="negative "+r$:gosub 500
60 no=int((len(n$)-1)/3)
270 return
70 rem pad left side
280 data "","ten","","thousand"
71 p=(no+1)*3-len(n$)
281 data "one","eleven","","million"
75 if p>0 then n$="0"+n$:p=p-1:goto 75
282 data "two","twelve","twenty","billion"
77 if val(n$)=0 then ou$=s$(0):gosub 500:goto 125
283 data "three","thirteen","thirty","trillion"
80 rem calculate left to right
284 data "four","fourteen","forty","quadrillion"
81 for i=no to 0 step -1:oi=no-i
285 data "five","fifteen","fifty","quintillion"
85 ch$=mid$(n$,1+(oi*3),3)
286 data "six","sixteen","sixty","sextillion"
90 h=val(mid$(ch$,1,1)):t=val(mid$(ch$,2,1)):s=val(mid$(ch$,3,1))
287 data "seven","seventeen","seventy","septillion"
93 if h=0 and t=0 and s=0 then goto 120
288 data "eight","eighteen","eighty","octillion"
95 if h>0 then ou$=s$(h)+" hundred ":gosub 500
289 data "nine","nineteen","ninety","nonillion"
100 if t>1 then ou$=t$(t)+mid$("- ",abs(s=0)+1,1):gosub 500
290 data "","","","decillion"
105 if t=1 then ou$=ts$(s)+" ":gosub 500
300 s=h-d*int(h/d)
110 if t<>1 and s>0 then ou$=s$(s)+" ":gosub 500
305 r$=u$(int(h/d))+mid$("-"+chr$(0),1+abs(s=0),1)+s$(s)+" "+r$
115 ou$=o$(i)+" ":gosub 500
310 return
120 next i
400 rem word-wrap string
125 print:print
401 tp$=r$
130 print "Another? (y/n) ";
405 if len(tp$)<=39 then goto 440
140 get k$:ifk$<>"y" and k$<>"n" then 140
410 for i=0 to 38:c$=mid$(tp$,39-i,1)
145 print k$
420 if s$<>" " then next i
150 if k$="y" then print:goto 40
430 as$=as$+left$(tp$,39-i)+chr$(13):tp$=mid$(tp$,40-i,len(tp$)):i=0:goto 405
200 end
440 as$=as$+tp$
500 rem print with word wrapping
450 r$=as$:return</lang>
505 cp=pos(0):nl=len(ou$)
510 if cp>co-nl then print
520 print ou$;
599 return
1000 data zero,ten,"",""
1001 data one,eleven,ten,thousand
1002 data two,twelve,twenty,million
1003 data three,thirteen,thirty,billion
1004 data four,fourteen,forty,trillion
1005 data five,fifteen,fifty,quadrillion
1006 data six,sixteen,sixty,quintillion
1007 data seven,seventeen,seventy,sextillion
1008 data eight,eighteen,eighty,septillion
1009 data nine,nineteen,ninety,octillion
1010 data "","","",nonillion
1011 data "","","",decillion</lang>
 
{{out}}
<pre>
give meEnter a number? 12345678901
one
one billion two-hundred thirty-four
million five hundred sixty-seven
another? (y/n) y
thousand eight hundred ninety.
Enter a number? 42
forty-two
another? (y/n) y
Enter a number? 9876543210
nine billion eight hundred seventy-six
million five hundred forty-three
thousand two hundred ten
another? (y/n) y
 
Enter a number? 134399592000000478293
one hundred thirty-four quintillion
three hundred ninety-nine quadrillion
five hundred ninety-two trillion
four hundred seventy-eight thousand
two hundred ninety-three
another? (y/n) y
Enter a number? 111222333444555000
one hundred eleven quadrillion
two hundred twenty-two trillion
three hundred thirty-three billion
four hundred forty-four million
five hundred fifty-five thousand
another? (y/n) n
ready.
&#9608;
</pre>
 
 
=={{header|Common Lisp}}==
113

edits