Number names: Difference between revisions

m (Fix Perl6 -> Raku links and comments)
Line 3,349:
 
=={{header|Lua}}==
===Original===
 
<lang lua>words = {"one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine "}
levels = {"thousand ", "million ", "billion ", "trillion ", "quadrillion ", "quintillion ", "sextillion ", "septillion ", "octillion ", [0] = ""}
Line 3,387:
 
if #vword == 0 then print "zero" else print(vword) end</lang>
 
===Alternate===
As used in [[Four is magic#Lua|Four is magic]]
<lang lua>-- Number names, in Lua, 6/17/2020 db
local oneslist = { [0]="", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }
local teenlist = { [0]="ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }
local tenslist = { [0]="", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }
local lionlist = { [0]="", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion", "decillion" }
local abs, floor = math.abs, math.floor
 
local function numname(num)
if (num == 0) then return "zero" end
local absnum, lion, result = abs(num), 0, ""
local function dashed(n) return n==0 and "" or "-" end
local function spaced(s) return s=="" and s or " "..s end
while (absnum > 0) do
local word, ones, tens, huns = "", absnum%10, floor(absnum/10)%10, floor(absnum/100)%10
if (tens==0) then word = oneslist[ones]
elseif (tens==1) then word = teenlist[ones]
else word = tenslist[tens] .. dashed(ones) .. oneslist[ones] end
if (huns > 0) then word = oneslist[huns] .. " hundred" .. spaced(word) end
if (word ~= "") then result = word .. spaced(lionlist[lion]) .. spaced(result) end
absnum = floor(absnum / 1000)
lion = lion + 1
end
if (num < 0) then result = "negative " .. result end
return result
end
 
local numbers = {
-1, 0, 1,
10, 15, 20, 21,
123, 300, 301, 320, 321,
1e3, 1e3+1, 1010, 1011, 1100, 1101, 1110, 1111, 1234, 4321, 12345, 54321, 123456, 654321,
1e6, 1e6+1, 1001000, 1234567, 12345678, 123456789, 987654321,
1e9, 1e9+1, 1000001000, 1001000000, 1001001000, 1234567890, 12345678901, 123456789012, 210987654321,
1e12, 1e12+1, 1000000001000, 1000001000000, 1000001001000, 1001001001001, 1234567890123, 12345678901234, 123456789012345, 543210987654321,
1e15, 1e15+1, 1010101010101010, 1234567890123456, 6543210987654321,
-- limit of precision, pushing it..
1e18, 1e21,
}
for _, num in ipairs(numbers) do
print( string.format("%.f: '%s'", num, numname(num)) )
end</lang>
{{out}}
<pre>-1: 'negative one'
0: 'zero'
1: 'one'
10: 'ten'
15: 'fifteen'
20: 'twenty'
21: 'twenty-one'
123: 'one hundred twenty-three'
300: 'three hundred'
301: 'three hundred one'
320: 'three hundred twenty'
321: 'three hundred twenty-one'
1000: 'one thousand'
1001: 'one thousand one'
1010: 'one thousand ten'
1011: 'one thousand eleven'
1100: 'one thousand one hundred'
1101: 'one thousand one hundred one'
1110: 'one thousand one hundred ten'
1111: 'one thousand one hundred eleven'
1234: 'one thousand two hundred thirty-four'
4321: 'four thousand three hundred twenty-one'
12345: 'twelve thousand three hundred forty-five'
54321: 'fifty-four thousand three hundred twenty-one'
123456: 'one hundred twenty-three thousand four hundred fifty-six'
654321: 'six hundred fifty-four thousand three hundred twenty-one'
1000000: 'one million'
1000001: 'one million one'
1001000: 'one million one thousand'
1234567: 'one million two hundred thirty-four thousand five hundred sixty-seven'
12345678: 'twelve million three hundred forty-five thousand six hundred seventy-eight'
123456789: 'one hundred twenty-three million four hundred fifty-six thousand seven hundred eighty-nine'
987654321: 'nine hundred eighty-seven million six hundred fifty-four thousand three hundred twenty-one'
1000000000: 'one billion'
1000000001: 'one billion one'
1000001000: 'one billion one thousand'
1001000000: 'one billion one million'
1001001000: 'one billion one million one thousand'
1234567890: 'one billion two hundred thirty-four million five hundred sixty-seven thousand eight hundred ninety'
12345678901: 'twelve billion three hundred forty-five million six hundred seventy-eight thousand nine hundred one'
123456789012: 'one hundred twenty-three billion four hundred fifty-six million seven hundred eighty-nine thousand twelve'
210987654321: 'two hundred ten billion nine hundred eighty-seven million six hundred fifty-four thousand three hundred twenty-one'
1000000000000: 'one trillion'
1000000000001: 'one trillion one'
1000000001000: 'one trillion one thousand'
1000001000000: 'one trillion one million'
1000001001000: 'one trillion one million one thousand'
1001001001001: 'one trillion one billion one million one thousand one'
1234567890123: 'one trillion two hundred thirty-four billion five hundred sixty-seven million eight hundred ninety thousand one hundred twenty-three'
12345678901234: 'twelve trillion three hundred forty-five billion six hundred seventy-eight million nine hundred one thousand two hundred thirty-four'
123456789012345: 'one hundred twenty-three trillion four hundred fifty-six billion seven hundred eighty-nine million twelve thousand three hundred forty-five'
543210987654321: 'five hundred forty-three trillion two hundred ten billion nine hundred eighty-seven million six hundred fifty-four thousand three hundred twenty-one'
1000000000000000: 'one quadrillion'
1000000000000001: 'one quadrillion one'
1010101010101010: 'one quadrillion ten trillion one hundred one billion ten million one hundred one thousand ten'
1234567890123456: 'one quadrillion two hundred thirty-four trillion five hundred sixty-seven billion eight hundred ninety million one hundred twenty-three thousand four hundred fifty-six'
6543210987654321: 'six quadrillion five hundred forty-three trillion two hundred ten billion nine hundred eighty-seven million six hundred fifty-four thousand three hundred twenty-one'
1000000000000000000: 'one quintillion'
1000000000000000000000: 'one sextillion'</pre>
 
=={{header|Maple}}==
Anonymous user