Modul:Formatnum
Documentația acestui modul poate fi creată la Modul:Formatnum/doc
local p = {}
local isNilOrEmpty= require('Modul:StringUtils').isNilOrEmpty
-- this function receives a number, tries to guess the layout and returns the
-- same number with the thousand separator stripped and the decimal separator
-- converted to English format
p._stripSeparators = function(input)
if isNilOrEmpty(input) then return nil end
local comma = ','
local dot = '%.'
local _, commaCount = string.gsub(input, comma, comma)
local _, dotCount = string.gsub(input, dot, dot)
if dotCount == 0 then
if commaCount == 0 then --plain old integer
return tonumber(input)
elseif commaCount == 1 then -- floating point, Romanian format
--TODO better guesswork
return tonumber(string.gsub(input, comma, dot), 10)
else -- integer with thousand separator
return tonumber(string.gsub(input, comma, ""), 10)
end
elseif dotCount == 1 then
if commaCount == 0 then --floating point, English format
--TODO better guesswork
return tonumber(input, 10)
elseif commaCount == 1 then --one each; the last one is decimal sep
local commaIndex = mw.ustring.find(input, comma)
local dotIndex = mw.ustring.find(input, dot)
if commaIndex > dotIndex then
return tonumber(string.gsub(string.gsub(input, dot, ""), comma, dot), 10)
else
return tonumber(string.gsub(input, comma, ""), 10)
end
else -- float with thousand separator comma
return tonumber(string.gsub(input, comma, ""), 10)
end
else
if commaCount == 0 then --integer with thousand separator
return tonumber(string.gsub(input, dot, ""), 10)
elseif commaCount == 1 then -- float with thousand separator dot
return tonumber(string.gsub(string.gsub(input, dot, ""), comma, dot), 10)
else -- invalid
return nil
end
end
end
function p.stripSeparators(frame)
ret = p._stripSeparators(frame.args[1])
if ret then return ret else return "" end
end
function p._beautify(lang, number)
if lang:getCode() ~= "ro" then return number end
number = mw.allToString(number)
number = string.gsub(number, "E%+([0-9]+)", " × 10<sup>%1</sup>")
number = string.gsub(number, "E([0-9]+)", " × 10<sup>%1</sup>")
number = string.gsub(number, "E%-([0-9]+)", " × 10<sup>-%1</sup>")
return number
end
function p._formatNum(number, lang, prec, compact)
mw.logObject(number, "number1")
if lang ~= nil and mw.language.isKnownLanguageTag(lang) == true then
lang = mw.getLanguage(lang)
end
if lang == nil or lang == {} or type(lang) == "string" then
lang = mw.getContentLanguage()
end
local processed = p._stripSeparators(number)
if processed == nil then return number end
-- Check the presence of an exponent (incorrectly managed in mw.language:FormatNum() and even forgotten due to an internal bug, e.g. in Hindi)
number = tostring(processed)
local pos = string.find(number, '[Ee]')
local exponent
if pos ~= nil then
exponent = string.sub(number, pos + 1, string.len(number))
number = string.sub(number, 1, pos - 1)
else
exponent = ''
end
number = tonumber(number)
mw.logObject(number, "number2")
-- TODO: do something with prec
if compact then
number = lang:formatNum(number, { noCommafy = 'y' }) -- caveat: can load localized resources for up to 20 languages
else
number = lang:formatNum(number) -- caveat: can load localized resources for up to 20 languages
end
-- Append the localized base-10 exponent without grouping separators (there's no reliable way to detect a localized leading symbol 'E')
if exponent ~= '' then
number = number .. 'E' .. tonumber(exponent)
end
return p._beautify(lang, number)
end
function p.formatNum(frame)
local args = require("Modul:Arguments").getArgs(frame)
local prec = args.prec or ''
local sep = args.sep or ''
local number = args[1] or args.number or ''
local lang = args[2] or args.lang or ''
return p._formatNum(number, lang, prec, sep ~= '')
end
return p