Module:Spellnum per MOS and Module:Spellnum per MOS/sandbox: Difference between pages
Appearance
(Difference between pages)
Content deleted Content added
fix for input with commas |
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]) |
||
⚫ | |||
local pass_zero = "zero" |
|||
if args['zero'] ~= nil and args['zero'] ~= '' then |
|||
pass_zero = args['zero'] |
|||
end |
|||
⚫ | |||
if numeral < 0 or |
if numeral < 0 or |
||
math.fmod(numeral, 1) ~= 0 or |
math.fmod(numeral, 1) ~= 0 or |
||
Line 22: | Line 18: | ||
end |
end |
||
-- Convert numeral to words |
|||
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']}} |
||
-- 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 ({"%. |
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) |