Module:RarityHandler: Difference between revisions

No edit summary
No edit summary
(One intermediate revision by the same user not shown)
Line 5: Line 5:
     if not rarity or type(rarity) ~= "string" then return nil end
     if not rarity or type(rarity) ~= "string" then return nil end
     rarity = mw.text.trim(rarity)
     rarity = mw.text.trim(rarity)
    -- Strip leading ~
     if rarity:sub(1, 1) == "~" then
     if rarity:sub(1, 1) == "~" then
         rarity = rarity:sub(2)
         rarity = rarity:sub(2)
     end
     end
    -- Strip commas
    rarity = rarity:gsub(",", "")
     return rarity
     return rarity
end
end
Line 71: Line 74:
     if cleaned == "1" then
     if cleaned == "1" then
         return "Always"
         return "Always"
    end
    -- Format numbers like 1/10000 → 1/10,000
    local prefix, num = cleaned:match("^(1/)(%d+%.?%d*)$")
    if prefix and num then
        local withCommas = mw.language.getContentLanguage():formatNum(tonumber(num))
        return (hasTilde and "~" or "") .. prefix .. withCommas
     end
     end