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.
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.
'''Machine specific notes for the word wrap feature:'''
Set <tt>'''co'''</tt> on line 20 for the following Commodore machines:
* VIC-20: <tt>co=21</tt>.
* Commodore 64 or Plus 4: No change.
* PET: <tt>co=peek(213)</tt>. (Will detect 40 or 80 column mode.)
* CBM-II variants: <tt>co=peek(223)</tt>. (Will detect 40 or 80 column mode.)
* Commodore 128: <tt>co=rwindow(2)-1</tt>. (Will detect 40 or 80 column mode.)
<lang commodorebasicv2>10 print chr$(147);chr$(14)
20 dim s$(11),ts$(11),t$(11),o$(11):co=39
30 for i=0 to 11:read s$(i),ts$(i),t$(i),o$(i):next
40 print "Enter a number";:input n$
45 ou$=""
50 if len(n$)>36 then print:print "Too long.":print:goto 40
51 print
55 rem check for negative
60 no=int((len(n$)-1)/3)
70 rem pad left side
71 p=(no+1)*3-len(n$)
75 if p>0 then n$="0"+n$:p=p-1:goto 75
77 if val(n$)=0 then ou$=s$(0):gosub 500:goto 125
80 rem calculate left to right
81 for i=no to 0 step -1:oi=no-i
85 ch$=mid$(n$,1+(oi*3),3)
90 h=val(mid$(ch$,1,1)):t=val(mid$(ch$,2,1)):s=val(mid$(ch$,3,1))
93 if h=0 and t=0 and s=0 then goto 120
95 if h>0 then ou$=s$(h)+" hundred ":gosub 500
100 if t>1 then ou$=t$(t)+mid$("- ",abs(s=0)+1,1):gosub 500
105 if t=1 then ou$=ts$(s)+" ":gosub 500
110 if t<>1 and s>0 then ou$=s$(s)+" ":gosub 500
115 ou$=o$(i)+" ":gosub 500
120 next i
125 print:print
130 print "Another? (y/n) ";
140 get k$:ifk$<>"y" and k$<>"n" then 140
145 print k$
150 if k$="y" then print:goto 40
200 end
500 rem print with word wrapping
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>
one
another? (y/n) y
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.
█
</pre>
=={{header|Common Lisp}}==
|