Number names: Difference between revisions

no edit summary
(Didn't like the adaptation of the Applesoft example. Rewrote a new example processing the number input as a string.)
No edit summary
Line 3,054:
1234000000745003 = one quadrillion, two hundred and thirty-four trillion, seven hundred and forty-five thousand and three
-9223372036854775808 = minus nine quintillion, two hundred and twenty-three quadrillion, three hundred and seventy-two trillion, thirty-six billion, eight hundred and fifty-four million, seven hundred and seventy-five thousand, eight hundred and eight
</pre>
 
=={{header|Liberty BASIC}}==
<lang lb>
global outnum$
dim ones$(20),tens$(9),gr$(5),group(5)
for a = 0 to 19
read a$:ones$(a)=a$
next a
for a = 1 to 8
read a$:tens$(a)=a$
next a
 
[start]
redim gr$(5)
redim group(5)
 
input "Enter a number (nonzero positive integers only less than 1 quadrillion, no commas): ";num$
 
num$=trim$(num$)
numVal=int(val(num$))
if numVal=0 or numVal>999999999999999 then print "Ended":end
 
numParse = numVal
numLen = len(str$(numParse))
 
if numLen<=15 then groupCount=5
if numLen<=12 then groupCount=4
if numLen<=9 then groupCount=3
if numLen<=6 then groupCount=2
if numLen<=3 then groupCount=1
 
if numLen>12 and numLen<=15 then
group(5)=int(numParse/1000000000000)
call convert group(5)," trillion"
gr$(5)=outnum$
numParse=nP(numParse,12)
numLen=len(str$(numParse))
end if
 
if numLen>9 and numLen<=12 then
group(4)=int(numParse/1000000000)
call convert group(4)," billion"
gr$(4)=outnum$
numParse=nP(numParse,9)
numLen=len(str$(numParse))
end if
 
if numLen>6 and numLen<=9 then
group(3)=int(numParse/1000000)
call convert group(3)," million"
gr$(3)=outnum$
numParse=nP(numParse,6)
numLen=len(str$(numParse))
end if
 
if numLen>3 and numLen<=6 then
group(2)=int(numParse/1000)
call convert group(2)," thousand"
gr$(2)=outnum$
numParse=nP(numParse,3)
numLen=len(str$(numParse))
end if
 
if numLen<=3 then
group(1)=numParse
call convert group(1),""
gr$(1)=outnum$
numLen=len(str$(numParse))
end if
 
print
for a=groupCount to 1 step -1
print gr$(a);" ";
next a
print
print
 
goto [start]
 
sub convert num, dem$
outnum$=""
for a=len(str$(num)) to 1 step -1
b$=mid$(str$(num),a,1)
c=val(b$)
d=len(str$(num))-a+1
select case
case d=1
outnum$=ones$(c)
case d=2
if c<2 then
outnum$=ones$(val(right$(str$(num),2)))
else
if c>=2 and val(right$(str$(num),1))<>0 then
outnum$=tens$(c-1)+"-"+outnum$
else
outnum$=tens$(c-1)
end if
end if
case d=3
if c<>0 then outnum$=ones$(c)+" hundred "+outnum$
end select
next a
outnum$=outnum$+dem$
end sub
 
function nP(num,L)
nP=val(right$(str$(num),L))
end function
 
data "","one","two","three","four","five","six","seven","eight","nine"
data "ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"
data "twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"
</lang>
{{out}}
<pre>
Entering 789456019600009 yields
seven hundred eighty-nine trillion four hundred fifty-six billion nineteen million six hundred thousand nine
</pre>