Jump to content

Module:Spellnum per MOS

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Swpb (talk | contribs) at 15:27, 14 May 2018. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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