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 GKFX (talk | contribs) at 10:38, 1 April 2021 (Refactor to allow use by other modules and only expand the template once.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

-- For use by other scripts. Takes arguments:
-- - 1: string or number, value to convert
-- - forcenum: string for Template:Yesno, forces a result in digits for all n ≥ 10.
-- - formating options for spellnum: zero, adj, ord, us
-- WARNING: zero defaults to blank which is probably not what you want.
function p.spellnum(args)
	local frame = mw.getCurrentFrame()
	local numeral = tonumber(args[1])

	-- Always return as digits for negative numbers, non-integers, and if (forcenum and numeral >= 10).
	if numeral < 0 or
			math.fmod(numeral, 1) ~= 0 or
			(numeral >= 10 and frame:expandTemplate{ title = 'yesno', args = {args['forcenum']} } == 'yes') then
		return tostring(numeral)
	end

	local spelled = frame:expandTemplate{ title = 'spellnum', args = {
		numeral, zero = args['zero'], adj = args['adj'], ord = args['ord'], us = args['us']}}
	
	if mw.ustring.find(spelled,'%a+[ %-]%a+[ %-]%a+') then
		return tostring(numeral)
	else
		return spelled
	end
end

function p.main(frame)
	return p.spellnum(frame.args)
end

return p