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 19:57, 11 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 = 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
--thousands and hundreds
			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
			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
--ones
			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