Sorry, this would work better with numbers over 100:
local floor = math.floor
local num = {{'one', 1}, {'two', 2}, {'three', 3}, {'four', 4}, {'five', 5}, {'six', 6}, {'seven', 7}, {'eight', 8}, {'nine', 9}, {'ten', 10}, {'eleven', 11},
{'twelve', 12}, {'thirteen', 13}, {'fourteen', 14}, {'fifteen', 15}, {'sixteen', 16}, {'seventeen', 17}, {'eighteen', 18}, {'nineteen', 19}}
local tens = {{'twenty', 20}, {'thirty', 30}, {'forty', 40}, {'fifty', 50}, {'sixty', 60}, {'seventy', 70}, {'eighty', 80}, {'ninety', 90}}
local bases = {{floor(1e18), ' quintillion'}, {floor(1e15), ' quadrillion'}, {floor(1e12), ' trillion'},
{' billion', floor(1e9)}, {' million', 1000000}, {' thousand', 1000}, {' hundred', 100}}
function wordToNumber(word)
for i,v in pairs(tens) do
for x,d in pairs(num) do
if word == num[x][1] then
return num[x][2]
end
if word == tens[i][1] then
return tens[i][2]
end
if word == tens[i][1].."-"..num[x][1] then
return tens[i][2]+num[x][2]
end
for z,b in pairs(bases) do
if word == num[x][1]..bases[z][1] then
return num[x][2] * bases[z][2]
end
if word == tens[i][1]..bases[z][1] then
return tens[i][2] * bases[z][2]
end
for h,a in pairs(num) do
if word == num[x][1]..bases[z][1].." "..num[h][1] then
return (num[x][2] * bases[z][2]) + num[h][2]
end
end
if word == tens[i][1].."-"..num[x][1]..bases[z][1] then
return (tens[i][2] + num[x][2]) * bases[z][2]
end
end
end
end
end
print(wordToNumber("twenty-one million"))
print(wordToNumber("one million"))
print(wordToNumber("fifty-one"))
print(wordToNumber("thirty"))
print(wordToNumber("six"))
print(wordToNumber("five hundred"))
print(wordToNumber("five hundred five"))