Toggle menu
15
236
81
27.8K
Kenshi Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.
Revision as of 22:34, 27 February 2025 by Prd (talk | contribs) (Created page with "local p = {} local words = {"thousand", "million", "billion", "trillion"} -- We don't need to go higher than this, no-one knows what an octillion is. -- 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(a...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


{{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 = {}
local words = {"thousand", "million", "billion", "trillion"} -- We don't need to go higher than this, no-one knows what an octillion is.

-- 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 mw.language.getContentLanguage():formatNum(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
		if numeral >= 1000000 and numeral <= 1000000000000000 then
			local size = math.min(4, math.floor(math.log10(numeral) / 3))
			numeral = numeral / 1000^size
			return ({"%.2f ", "%.1f ", "%d "})[1 + math.floor(math.log10(numeral))]:format(numeral) .. words[size]
		end
		return mw.language.getContentLanguage():formatNum(numeral)
	else 
		return spelled
	end
end

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

return p
Contents