RosettaCodeData/Task/Number-names/Phix/number-names.phix
2020-02-17 23:21:07 -08:00

104 lines
2.9 KiB
Text

-- demo/rosetta/Number_names.exw
constant twenties = {"zero","one","two","three","four","five","six","seven","eight","nine","ten",
"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}
function Twenty(integer N)
return twenties[mod(N,20)+1]
end function
constant decades = {"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}
function Decade(integer N)
return decades[mod(N,10)-1]
end function
function Hundred(integer N)
if N<20 then
return Twenty(N)
elsif mod(N,10)=0 then
return Decade(mod(floor(N/10),10))
end if
return Decade(floor(N/10)) & '-' & Twenty(mod(N,10))
end function
function Thousand(integer N, string withand)
if N<100 then
return withand & Hundred (N);
elsif mod(N,100)=0 then
return withand & Twenty(floor(N/100))&" hundred"
end if
return Twenty(floor(N/100)) & " hundred and " & Hundred(mod(N,100))
end function
constant orders = {{power(10,15),"quadrillion"},
{power(10,12),"trillion"},
{power(10,9),"billion"},
{power(10,6),"million"},
{power(10,3),"thousand"}}
function Triplet(atom N)
atom Order, High, Low
string Name, res = ""
integer n
for i=1 to length(orders) do
{Order,Name} = orders[i]
High = floor(N/Order)
Low = mod(N,Order)
if High!=0 then
res &= Thousand(High,"")&' '&Name
end if
N = Low
if Low=0 then exit end if
if length(res) and High!=0 then
res &= ", "
end if
end for
if N!=0 or res="" then
res &= Thousand(floor(N),iff(res=""?"":"and "))
N = abs(mod(N,1))
if N>1e-6 then
res &= " point"
for i=1 to 10 do
n = floor(N*10.0000001)
res &= ' '&twenties[n+1]
N = N*10-n
if abs(N)<1e-6 then exit end if
end for
end if
end if
return res
end function
global function spell(atom N)
string res = ""
if N<0 then
res = "minus "
N = -N
end if
res &= Triplet(N)
return res
end function
global
constant Samples = {99, 300, 310, 417,1_501, 12_609, 200000000000100, 999999999999999,
-123456787654321,102003000400005,1020030004,102003,102,1,0,-1,-99,
-1501,1234,12.34,10000001.2,1E-3,-2.7182818,
201021002001,-20102100200,2010210020,-201021002,20102100,-2010210,
201021,-20102,2010,-201,20,-2}
global function smartp(atom N)
string res
if N=floor(N) then return sprintf("%d",N) end if
res = sprintf("%18.8f",N)
if find('.',res) then
res = trim_tail(res,"0")
end if
return res
end function
if include_file()==1 then
for i=1 to length(Samples) do
atom si = Samples[i]
printf(1,"%18s %s\n",{smartp(si),spell(si)})
end for
end if