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 20:23, 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 numeral for intermediate cases

	output = ''

	if numeral<0 or math.fmod(numeral,1)~=0 then
		output = numeral
	elseif force and numeral>9 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
			output = numeral
		else
--thousands
			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
				output = numeral
			else
--hundreds
				if sigfigs>=100 then
					sigfigs = sigfigs/100
					words = words + 1
					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
					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
					output2 = output2 .. ones_words[sigfigs]
				end
						
				output = output2 .. output
			end
		end
		if words>2 then output = numeral end
	end
--if numerals are preferred
--	if force then
--		if numeral>9 then
--			return numeral
--		else
--			return output
--		end
--	else
--		return output
--	end
	return output
end

return p