<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wicri-demo.istex.fr/Wicri/SitDemIstex/V131/pool/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://wicri-demo.istex.fr/Wicri/SitDemIstex/V131/pool/index.php?action=history&amp;feed=atom&amp;title=Module%3AFormatnum"/>
	<link rel="alternate" type="text/html" href="https://wicri-demo.istex.fr/Wicri/SitDemIstex/V131/pool/index.php?title=Module:Formatnum&amp;action=history"/>
	<updated>2026-04-19T11:13:34Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.10</generator>
	<entry>
		<id>https://wicri-demo.istex.fr/Wicri/SitDemIstex/V131/pool/index.php?title=Module:Formatnum&amp;diff=10909&amp;oldid=prev</id>
		<title>Jacques Ducloy: Created page with &quot;-- This module is intended to replace the functionality of Template:Formatnum and related templates. local p = {}  function p.main(frame)     local args = frame:getParent().ar...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wicri-demo.istex.fr/Wicri/SitDemIstex/V131/pool/index.php?title=Module:Formatnum&amp;diff=10909&amp;oldid=prev"/>
		<updated>2018-11-02T08:59:45Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;-- This module is intended to replace the functionality of Template:Formatnum and related templates. local p = {}  function p.main(frame)     local args = frame:getParent().ar...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;-- This module is intended to replace the functionality of Template:Formatnum and related templates.&lt;br /&gt;
local p = {}&lt;br /&gt;
&lt;br /&gt;
function p.main(frame)&lt;br /&gt;
    local args = frame:getParent().args&lt;br /&gt;
    local prec    = args.prec or ''&lt;br /&gt;
    local sep     = args.sep or ''&lt;br /&gt;
    local number  = args[1] or args.number or ''&lt;br /&gt;
    local lang    = args[2] or args.lang or ''&lt;br /&gt;
    -- validate the language parameter within MediaWiki's caller frame&lt;br /&gt;
    if lang == &amp;quot;arabic-indic&amp;quot; then -- only for back-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 == '' or not mw.language.isSupportedLanguage(lang) then&lt;br /&gt;
        -- Note that 'SupportedLanguages' are not necessarily 'BuiltinValidCodes', and so they are not necessarily&lt;br /&gt;
        -- 'KnownLanguages' (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's language,&lt;br /&gt;
        -- instead of the local 'ContentLanguage' of the Wiki.&lt;br /&gt;
        lang = frame:callParserFunction( &amp;quot;int&amp;quot;, &amp;quot;lang&amp;quot; ) -- get user's chosen language&lt;br /&gt;
    end&lt;br /&gt;
    return p.formatNum(number, lang, prec, sep ~= '')&lt;br /&gt;
end&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;] = { '൦', '൧', '൨', '൩', '൪', '൫', '൬', '൭', '൮', '൯' },&lt;br /&gt;
    [&amp;quot;mn&amp;quot;]     = { '᠐', '᠑', '᠒', '᠓', '᠔', '᠕', '᠖', '᠗', '᠘', '᠙'},&lt;br /&gt;
    [&amp;quot;ta&amp;quot;]     = { '௦', '௧', '௨', '௩', '௪', '௫', '௬', '௭', '௮', '௯'},&lt;br /&gt;
    [&amp;quot;te&amp;quot;]     = { '౦', '౧', '౨', '౩', '౪', '౫', '౬', '౭', '౮', '౯'},&lt;br /&gt;
    [&amp;quot;th&amp;quot;]     = { '๐', '๑', '๒', '๓', '๔', '๕', '๖', '๗', '๘', '๙'}&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, '[Ee]')&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 = ''&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;
    if (prec or 0) &amp;gt; 0 then&lt;br /&gt;
        pos = string.find(number, '.', 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 = '' -- already present&lt;br /&gt;
        else&lt;br /&gt;
            dot = '.' -- must be added&lt;br /&gt;
        end&lt;br /&gt;
    else&lt;br /&gt;
        dot = '' -- don't add dot&lt;br /&gt;
        prec = 0 -- don't alter the precision&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if lang ~= nil and mw.language.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 = 'y' }) -- 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 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's no reliable way to detect a localized leading symbol 'E')&lt;br /&gt;
        if exponent ~= '' then&lt;br /&gt;
            number = number .. 'E' .. language:formatNum(tonumber(exponent),{noCommafy=true})&lt;br /&gt;
        end&lt;br /&gt;
    else -- not localized, ASCII only&lt;br /&gt;
        -- Postprocessing the precision&lt;br /&gt;
        if prec &amp;gt; 0 then&lt;br /&gt;
            number = number .. dot .. mw.string.rep('0', 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 ~= '' then&lt;br /&gt;
            number = number .. 'E' .. 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;
return p&lt;/div&gt;</summary>
		<author><name>Jacques Ducloy</name></author>
		
	</entry>
</feed>