Module:Spellnum per MOS and Module:Spellnum per MOS/sandbox: Difference between pages

(Difference between pages)
Jump to navigation Jump to search
Page 1
Page 2
imported>Hike395
if fed non-numeric string, just return it
 
imported>GKFX
Create sandbox version of Module:Spellnum per MOS
 
Line 6: Line 6:
-- - forcenum: string for Template:Yesno, forces a result in digits for all n ≥ 10.
-- - forcenum: string for Template:Yesno, forces a result in digits for all n ≥ 10.
-- - formating options for spellnum: zero, adj, ord, us
-- - formating options for spellnum: zero, adj, ord, us
-- WARNING: zero defaults to blank which is probably not what you want.
function p.spellnum(args)
function p.spellnum(args)
local frame = mw.getCurrentFrame()
local frame = mw.getCurrentFrame()
local numeral = tonumber((string.gsub(args[1], ",", "")))
local numeral = tonumber(args[1])
    if not numeral then
        return args[1]
    end
    local pass_zero = "zero"
-- Always return as digits for negative numbers, non-integers, and if (forcenum and numeral >= 10).
    if args['zero'] ~= nil and args['zero'] ~= '' then
    pass_zero = args['zero']
    end
   
-- Always return numerals for negative numbers, non-integers, and if (forcenum and numeral >= 10).
if numeral < 0 or
if numeral < 0 or
math.fmod(numeral, 1) ~= 0 or
math.fmod(numeral, 1) ~= 0 or
Line 25: Line 18:
end
end


-- Convert numeral to words
local spelled = frame:expandTemplate{ title = 'spellnum', args = {
local spelled = frame:expandTemplate{ title = 'spellnum', args = {
numeral, zero = pass_zero, adj = args['adj'], ord = args['ord'], us = args['us']}}
numeral, zero = args['zero'], adj = args['adj'], ord = args['ord'], us = args['us']}}
-- Return numerals if more than two words would be needed, else return words
if mw.ustring.find(spelled,'%a+[ %-]%a+[ %-]%a+') then
if mw.ustring.find(spelled,'%a+[ %-]%a+[ %-]%a+') then
-- Handle numbers larger than one million
if numeral >= 1000000 and numeral <= 1000000000000000 then
if numeral >= 1000000 and numeral <= 1000000000000000 then
local size = math.min(4, math.floor(math.log10(numeral) / 3))
local size = math.min(4, math.floor(math.log10(numeral) / 3))
numeral = numeral / 1000^size
numeral = numeral / 1000^size
return ({"%.1f ", "%d ", "%d "})[1 + math.floor(math.log10(numeral))]:format(numeral) .. words[size]
return ({"%.2f ", "%.1f ", "%d "})[1 + math.floor(math.log10(numeral))]:format(numeral) .. words[size]
end
end
return mw.language.getContentLanguage():formatNum(numeral)
return mw.language.getContentLanguage():formatNum(numeral)