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 = frame.args[2] -- Force conversion to text for intermediate cases
output = ''
if numeral<0 or math.fmod(numeral,1)~=0 then
output = numeral
elseif numeral==0 then output='zero'
else
--
local words = 0
local sigfigs = numeral
local thousands = 0
local hundreds = 0
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
else
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>100 then
sigfigs = sigfigs/100
words = words + 1
output = ' hundred' .. output
end
tens = (sigfigs - math.fmod(sigfigs,10))/10
tens_words = {'twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety'}
output2 = ''
if tens>=2 then output2 = tens_words[tens-1] end
sigfigs = sigfigs - 10*tens
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 output2 = output2 .. ones_words[sigfigs] end
output = output2 .. output
end
end
return output
end
return p