<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://mbwiki.stairwaygames.work/w/index.php?action=history&amp;feed=atom&amp;title=Module%3AFormatnum</id>
	<title>Module:Formatnum - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://mbwiki.stairwaygames.work/w/index.php?action=history&amp;feed=atom&amp;title=Module%3AFormatnum"/>
	<link rel="alternate" type="text/html" href="https://mbwiki.stairwaygames.work/w/index.php?title=Module:Formatnum&amp;action=history"/>
	<updated>2026-04-07T16:25:47Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://mbwiki.stairwaygames.work/w/index.php?title=Module:Formatnum&amp;diff=5689&amp;oldid=prev</id>
		<title>Admin coral island: Created page with &quot;-- &lt;pre&gt;This module formats numbers local p = {}  local digit = { -- substitution of decimal digits for languages not supported by mw.language:formatNum() in core Lua libraries for MediaWiki     [&quot;ml-old&quot;] = { &#039;൦&#039;, &#039;൧&#039;, &#039;൨&#039;, &#039;൩&#039;, &#039;൪&#039;, &#039;൫&#039;, &#039;൬&#039;, &#039;൭&#039;, &#039;൮&#039;, &#039;൯&#039; },     [&quot;mn&quot;]     = { &#039;᠐&#039;, &#039;᠑&#039;, &#039;᠒&#039;, &#039;᠓&#039;, &#039;᠔&#039;, &#039;᠕&#039;, &#039;᠖&#039;, &#039;᠗&#039;, &#039;᠘&#039;, &#039;᠙&#039;},     [&quot;ta&quot;]     = { &#039;௦&#039;, &#039;௧&#039;, &#039;௨&#039;, &#039;௩&#039;, &#039;௪&#039;, &#039;௫&#039;, &#039;௬&#039;, &#039;௭&#039;, &#039;௮&#039;, &#039;௯&#039;},     [&quot;te&quot;]...&quot;</title>
		<link rel="alternate" type="text/html" href="https://mbwiki.stairwaygames.work/w/index.php?title=Module:Formatnum&amp;diff=5689&amp;oldid=prev"/>
		<updated>2023-08-04T02:43:33Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;-- &amp;lt;pre&amp;gt;This module formats numbers local p = {}  local digit = { -- substitution of decimal digits for languages not supported by mw.language:formatNum() in core Lua libraries for MediaWiki     [&amp;quot;ml-old&amp;quot;] = { &amp;#039;൦&amp;#039;, &amp;#039;൧&amp;#039;, &amp;#039;൨&amp;#039;, &amp;#039;൩&amp;#039;, &amp;#039;൪&amp;#039;, &amp;#039;൫&amp;#039;, &amp;#039;൬&amp;#039;, &amp;#039;൭&amp;#039;, &amp;#039;൮&amp;#039;, &amp;#039;൯&amp;#039; },     [&amp;quot;mn&amp;quot;]     = { &amp;#039;᠐&amp;#039;, &amp;#039;᠑&amp;#039;, &amp;#039;᠒&amp;#039;, &amp;#039;᠓&amp;#039;, &amp;#039;᠔&amp;#039;, &amp;#039;᠕&amp;#039;, &amp;#039;᠖&amp;#039;, &amp;#039;᠗&amp;#039;, &amp;#039;᠘&amp;#039;, &amp;#039;᠙&amp;#039;},     [&amp;quot;ta&amp;quot;]     = { &amp;#039;௦&amp;#039;, &amp;#039;௧&amp;#039;, &amp;#039;௨&amp;#039;, &amp;#039;௩&amp;#039;, &amp;#039;௪&amp;#039;, &amp;#039;௫&amp;#039;, &amp;#039;௬&amp;#039;, &amp;#039;௭&amp;#039;, &amp;#039;௮&amp;#039;, &amp;#039;௯&amp;#039;},     [&amp;quot;te&amp;quot;]...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;-- &amp;lt;pre&amp;gt;This module formats numbers&lt;br /&gt;
local p = {}&lt;br /&gt;
&lt;br /&gt;
local digit = { -- substitution of decimal digits for languages not supported by mw.language:formatNum() in core Lua libraries for MediaWiki&lt;br /&gt;
    [&amp;quot;ml-old&amp;quot;] = { &amp;#039;൦&amp;#039;, &amp;#039;൧&amp;#039;, &amp;#039;൨&amp;#039;, &amp;#039;൩&amp;#039;, &amp;#039;൪&amp;#039;, &amp;#039;൫&amp;#039;, &amp;#039;൬&amp;#039;, &amp;#039;൭&amp;#039;, &amp;#039;൮&amp;#039;, &amp;#039;൯&amp;#039; },&lt;br /&gt;
    [&amp;quot;mn&amp;quot;]     = { &amp;#039;᠐&amp;#039;, &amp;#039;᠑&amp;#039;, &amp;#039;᠒&amp;#039;, &amp;#039;᠓&amp;#039;, &amp;#039;᠔&amp;#039;, &amp;#039;᠕&amp;#039;, &amp;#039;᠖&amp;#039;, &amp;#039;᠗&amp;#039;, &amp;#039;᠘&amp;#039;, &amp;#039;᠙&amp;#039;},&lt;br /&gt;
    [&amp;quot;ta&amp;quot;]     = { &amp;#039;௦&amp;#039;, &amp;#039;௧&amp;#039;, &amp;#039;௨&amp;#039;, &amp;#039;௩&amp;#039;, &amp;#039;௪&amp;#039;, &amp;#039;௫&amp;#039;, &amp;#039;௬&amp;#039;, &amp;#039;௭&amp;#039;, &amp;#039;௮&amp;#039;, &amp;#039;௯&amp;#039;},&lt;br /&gt;
    [&amp;quot;te&amp;quot;]     = { &amp;#039;౦&amp;#039;, &amp;#039;౧&amp;#039;, &amp;#039;౨&amp;#039;, &amp;#039;౩&amp;#039;, &amp;#039;౪&amp;#039;, &amp;#039;౫&amp;#039;, &amp;#039;౬&amp;#039;, &amp;#039;౭&amp;#039;, &amp;#039;౮&amp;#039;, &amp;#039;౯&amp;#039;},&lt;br /&gt;
    [&amp;quot;th&amp;quot;]     = { &amp;#039;๐&amp;#039;, &amp;#039;๑&amp;#039;, &amp;#039;๒&amp;#039;, &amp;#039;๓&amp;#039;, &amp;#039;๔&amp;#039;, &amp;#039;๕&amp;#039;, &amp;#039;๖&amp;#039;, &amp;#039;๗&amp;#039;, &amp;#039;๘&amp;#039;, &amp;#039;๙&amp;#039;}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
function p.main(frame)&lt;br /&gt;
    local args = require(&amp;quot;Dev:Arguments&amp;quot;).getArgs(frame)&lt;br /&gt;
    local prec    = args.prec or &amp;#039;&amp;#039;&lt;br /&gt;
    local sep     = args.sep or &amp;#039;&amp;#039;&lt;br /&gt;
    local number  = args[1] or args.number or &amp;#039;&amp;#039;&lt;br /&gt;
    local lang    = args[2] or args.lang or &amp;#039;&amp;#039;&lt;br /&gt;
    -- validate the language parameter within MediaWiki&amp;#039;s caller frame&lt;br /&gt;
    if lang == &amp;quot;arabic-indic&amp;quot; then -- only for compatibility (&amp;quot;arabic-indic&amp;quot; is not a SupportedLanguage)&lt;br /&gt;
        lang = &amp;quot;fa&amp;quot; -- better support than &amp;quot;ks&amp;quot;&lt;br /&gt;
    elseif lang == &amp;#039;&amp;#039; or (not isKnownLanguageTag(lang) and not digit.lang) then&lt;br /&gt;
        -- Note that &amp;#039;SupportedLanguages&amp;#039; are not necessarily &amp;#039;BuiltinValidCodes&amp;#039;, and so they are not necessarily&lt;br /&gt;
        -- &amp;#039;KnownLanguages&amp;#039; (with a language name defined at least in the default localisation of the local wiki).&lt;br /&gt;
        -- But they all are ValidLanguageCodes (suitable as Wiki subpages or identifiers: no slash, colon, HTML tags, or entities)&lt;br /&gt;
        -- In addition, they do not contain any capital letter in order to be unique in page titles (restriction inexistant in BCP47),&lt;br /&gt;
        -- but they may violate the standard format of BCP47 language tags for specific needs in MediaWiki.&lt;br /&gt;
        -- Empty/unspecified and unsupported languages are treated here in Commons using the user&amp;#039;s language,&lt;br /&gt;
        -- instead of the local &amp;#039;ContentLanguage&amp;#039; of the Wiki.&lt;br /&gt;
        lang =  &amp;#039;en&amp;#039;&lt;br /&gt;
    end&lt;br /&gt;
    return p.formatNum(number, lang, prec, sep ~= &amp;#039;&amp;#039;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function p.formatNum(number, lang, prec, compact)&lt;br /&gt;
&lt;br /&gt;
    -- Do not alter the specified value when it is not a valid number, return it as is&lt;br /&gt;
    local value = tonumber(number)&lt;br /&gt;
    if value == nil then&lt;br /&gt;
        return number&lt;br /&gt;
    end&lt;br /&gt;
    -- Basic ASCII-only formatting (without paddings)&lt;br /&gt;
    number = tostring(value)&lt;br /&gt;
&lt;br /&gt;
    -- Check the presence of an exponent (incorrectly managed in mw.language:FormatNum() and even forgotten due to an internal bug, e.g. in Hindi)&lt;br /&gt;
    local exponent&lt;br /&gt;
    local pos = string.find(number, &amp;#039;[Ee]&amp;#039;)&lt;br /&gt;
    if pos ~= nil then&lt;br /&gt;
        exponent = string.sub(number, pos + 1, string.len(number))&lt;br /&gt;
        number = string.sub(number, 1, pos - 1)&lt;br /&gt;
    else&lt;br /&gt;
        exponent = &amp;#039;&amp;#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    -- Check the minimum precision requested&lt;br /&gt;
    prec = tonumber(prec) -- nil if not specified as a true number&lt;br /&gt;
    if prec ~= nil then&lt;br /&gt;
        prec = math.floor(prec)&lt;br /&gt;
        if prec &amp;lt; 0 then&lt;br /&gt;
            prec = nil -- discard an incorrect precision (not a positive integer)&lt;br /&gt;
        elseif prec &amp;gt; 14 then&lt;br /&gt;
            prec = 14 -- maximum precision supported by tostring(number)&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    -- Preprocess the minimum precision in the ASCII string&lt;br /&gt;
    local dot&lt;br /&gt;
    local truncateDecimals = false&lt;br /&gt;
&lt;br /&gt;
    if prec == 0 then&lt;br /&gt;
    	truncateDecimals = true&lt;br /&gt;
    elseif (prec or 0) &amp;gt; 0 then&lt;br /&gt;
        pos = string.find(number, &amp;#039;.&amp;#039;, 1, true) -- plain search, no regexp&lt;br /&gt;
        if pos ~= nil then&lt;br /&gt;
            prec = pos + prec - string.len(number) -- effective number of trailing decimals to add or remove&lt;br /&gt;
            dot = &amp;#039;&amp;#039; -- already present&lt;br /&gt;
        else&lt;br /&gt;
            dot = &amp;#039;.&amp;#039; -- must be added&lt;br /&gt;
        end&lt;br /&gt;
    else&lt;br /&gt;
        dot = &amp;#039;&amp;#039; -- don&amp;#039;t add dot&lt;br /&gt;
        prec = 0 -- don&amp;#039;t alter the precision&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if lang ~= nil and isKnownLanguageTag(lang) == true then&lt;br /&gt;
        -- Convert number to localized digits, decimal separator, and group separators&lt;br /&gt;
        local language = mw.getLanguage(lang)&lt;br /&gt;
        if compact then&lt;br /&gt;
            number = language:formatNum(tonumber(number), { noCommafy = &amp;#039;y&amp;#039; }) -- caveat: can load localized resources for up to 20 languages&lt;br /&gt;
        else&lt;br /&gt;
            number = language:formatNum(tonumber(number)) -- caveat: can load localized resources for up to 20 languages&lt;br /&gt;
        end&lt;br /&gt;
        -- Postprocessing the precision&lt;br /&gt;
        if truncateDecimals then&lt;br /&gt;
        	pos = string.find(number, &amp;#039;.&amp;#039;, 1, true)&lt;br /&gt;
        	if pos ~= nil then&lt;br /&gt;
        		number = mw.ustring.sub(number, 1, pos - 1)&lt;br /&gt;
        	end&lt;br /&gt;
        elseif prec &amp;gt; 0 then&lt;br /&gt;
            local zero = language:formatNum(0)&lt;br /&gt;
            number = number .. dot .. mw.ustring.rep(zero, prec)&lt;br /&gt;
        elseif prec &amp;lt; 0 then&lt;br /&gt;
            -- TODO: rounding of last decimal; here only truncate decimals in excess&lt;br /&gt;
            number = mw.ustring.sub(number, 1, mw.ustring.len(number) + prec)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        -- Append the localized base-10 exponent without grouping separators (there&amp;#039;s no reliable way to detect a localized leading symbol &amp;#039;E&amp;#039;)&lt;br /&gt;
        if exponent ~= &amp;#039;&amp;#039; then&lt;br /&gt;
            number = number .. &amp;#039;E&amp;#039; .. mw.ustring.gsub(language:formatNum(tonumber(exponent)), groupsep, &amp;#039;&amp;#039;)&lt;br /&gt;
        end&lt;br /&gt;
    else -- not localized, ASCII only&lt;br /&gt;
        -- Postprocessing the precision&lt;br /&gt;
        if truncateDecimals then&lt;br /&gt;
        	pos = string.find(number, &amp;#039;.&amp;#039;, 1, true)&lt;br /&gt;
			if pos ~= nil then&lt;br /&gt;
        		number = mw.ustring.sub(number, 1, pos - 1)&lt;br /&gt;
        	end&lt;br /&gt;
        elseif prec &amp;gt; 0 then&lt;br /&gt;
            number = number .. dot .. mw.string.rep(&amp;#039;0&amp;#039;, prec)&lt;br /&gt;
        elseif prec &amp;lt; 0 then&lt;br /&gt;
            -- TODO: rounding of last decimal; here only truncate decimals in excess&lt;br /&gt;
            number = mw.string.sub(number, 1, mw.string.len(number) + prec)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        -- Append the base-10 exponent&lt;br /&gt;
        if exponent ~= &amp;#039;&amp;#039; then&lt;br /&gt;
            number = number .. &amp;#039;E&amp;#039; .. exponent&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    -- Special cases for substitution of ASCII digits (missing support in Lua core libraries for some languages)&lt;br /&gt;
    if digit[lang] then&lt;br /&gt;
        for i, v in ipairs(digit[lang]) do&lt;br /&gt;
            number = mw.ustring.gsub(number, tostring(i - 1), v)&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    return number&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function isKnownLanguageTag(langCode)&lt;br /&gt;
    local bool &lt;br /&gt;
    if type(langCode) ==&amp;quot;number&amp;quot; then&lt;br /&gt;
        return false&lt;br /&gt;
    end&lt;br /&gt;
    bool = (langCode and type(langCode)~=&amp;quot;number&amp;quot; and mw.language.isValidBuiltInCode( langCode ) and &lt;br /&gt;
        mw.language.fetchLanguageName( langCode )) ~= &amp;quot;&amp;quot; &lt;br /&gt;
    return  bool &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>Admin coral island</name></author>
	</entry>
</feed>