Module:Spellnum per MOS and Module:Spellnum per MOS/sandbox: Difference between pages
(Difference between pages)
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 | local numeral = tonumber(args[1]) | ||
-- Always return as digits 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 | ||
local spelled = frame:expandTemplate{ title = 'spellnum', args = { | local spelled = frame:expandTemplate{ title = 'spellnum', args = { | ||
numeral, zero = | numeral, zero = args['zero'], adj = args['adj'], ord = args['ord'], us = args['us']}} | ||
if mw.ustring.find(spelled,'%a+[ %-]%a+[ %-]%a+') then | if mw.ustring.find(spelled,'%a+[ %-]%a+[ %-]%a+') then | ||
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 ({"%. | 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) | ||