Module:Spellnum per MOS
Appearance
{{Module rating }}
Usage
Implements Template:Spellnum per MOS.
{{#invoke:Spellnum per MOS|main|number to format|if second arg equals 1, prefer numerals}}
For use by other Lua modules:
local spellnum = require('Module:Spellnum per MOS').spellnum
spellnum{ 11 } -- returns 'eleven'
local p = {}
-- Automate MOS:NUMERAL
function p.main(frame)
local numeral = tonumber(frame.args[1])
local force = tonumber(frame.args[2]) -- Force numeral for intermediate cases
if numeral<0 or math.fmod(numeral,1)~=0 or (force==1 and numeral>9) then
return numeral
end
if numeral==0 then
return 'zero'
end
output = ''
local words = 0
local sigfigs = numeral
local thousands = 0
local hundreds = 0
--thousands
while math.fmod(sigfigs,1000)==0 do
sigfigs = sigfigs/1000
thousands = thousands + 1
end
if sigfigs>100 and math.fmod(sigfigs,100)~=0 then
return numeral
end
if thousands>0 then
words = 1
thou_words = {'thousand','million','billion','trillion','quadrillion','quintillion','sextillion'}
if thousands<8 then output=' ' .. thou_words[thousands]
else output=' × 10<sup>' .. thousands*3 .. '</sup>'
end
end
if sigfigs>1000 then
return numeral
end
--hundreds
if sigfigs>=100 then
sigfigs = sigfigs/100
words = words + 1
if words>=2 then return numeral end
output = ' hundred' .. output
end
--20 to 99
tens = (sigfigs - math.fmod(sigfigs,10))/10
tens_words = {'twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety'}
output2 = ''
if tens>=2 then
words = words + 1
if words>=3 then return numeral end
output2 = tens_words[tens-1]
sigfigs = sigfigs - 10*tens
end
--1 to 19
ones_words = {'one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen'}
if tens>=2 and sigfigs>=0 then output2 = output2 .. '-' end
if sigfigs>0 then
words = words + 1
if words>=3 then return numeral end
output2 = output2 .. ones_words[sigfigs]
end
--output
output = output2 .. output
return output
end
return p