Jump to content
Toggle menu
  • 8 articles
  • 75 files
  • 7 users
  • 37.8K edits
Neodyland Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Formatnum: Difference between revisions

From Neodyland Wiki
m spelling
simpler version
Line 2: Line 2:
local p = {}
local p = {}


local digit = { -- languages not supported by the Lua of wikimedia software
["ml-old"] = { '൦', '൧', '൨', '൩', '൪', '൫', '൬', '൭', '൮', '൯' },
["mn"]    = {  '᠐', '᠑', '᠒', '᠓', '᠔', '᠕', '᠖', '᠗', '᠘', '᠙'},
["te"]    = { '౦', '౧', '౨', '౩', '౪', '౫', '౬', '౭', '౮', '౯'},
["th"]    = { '๐', '๑', '๒', '๓', '๔', '๕', '๖', '๗', '๘', '๙'}
}
function p.main(frame)
function p.main(frame)
local pframe = frame:getParent()
local pframe = frame:getParent()
local config = frame.args
local config = frame.args
local args = pframe.args
local prec  = pframe.args.prec
local number = pframe.args[1]
local lang  = string.lower(pframe.args[2])
local sep    = pframe.args.sep or ""
if not lang or not mw.language.isSupportedLanguage(lang) then
lang = frame:preprocess( "{{int:lang}}" )
end
return p._main(number, lang, sep, prec, config)
end
function p._main(number, lang, sep, prec, config)
-- Preprocessing prec
-- Preprocessing prec
if tonumber(args.prec) ~= nil and tonumber(args.prec) > 0 and tonumber(args[1]) ~= nil then
local precNum = tonumber(prec)
if string.find(tostring(args[1]), "%.") ~= nil then
if precNum ~= nil and precNum > 0 and tonumber(number) ~= nil then
if (string.len(args[1]) - string.find(tostring(args[1]), "%.")) < tonumber(args.prec) then
if string.find(tostring(number), "%.") ~= nil then
precnumber = "" .. args[1] .. string.rep("0", tonumber(args.prec) - (string.len(args[1]) - string.find(tostring(args[1]), "%.")))
if (string.len(number) - string.find(tostring(number), "%.")) < precNum then
elseif (string.len(args[1]) - string.find(tostring(args[1]), "%.")) > tonumber(args.prec) then
precnumber = "" .. number .. string.rep("0", precNum - (string.len(number) - string.find(tostring(number), "%.")))
local proc = string.gsub(args[1], "0+$", "")
elseif (string.len(number) - string.find(tostring(number), "%.")) > precNum then
if (string.len(proc) - string.find(tostring(proc), "%.")) < tonumber(args.prec) then
local proc = string.gsub(number, "0+$", "")
precnumber = proc .. string.rep("0", tonumber(args.prec) - (string.len(proc) - string.find(tostring(proc), "%.")))
if (string.len(proc) - string.find(tostring(proc), "%.")) < precNum then
precnumber = proc .. string.rep("0", precNum - (string.len(proc) - string.find(tostring(proc), "%.")))
else
else
precnumber = proc
precnumber = proc
end
end
elseif (string.len(args[1]) - string.find(tostring(args[1]), "%.")) == tonumber(args.prec) then
elseif (string.len(number) - string.find(tostring(number), "%.")) == precNum then
precnumber = args[1]
precnumber = number
end
end
else
else
precnumber = "" .. args[1] .. "." .. string.rep("0", args.prec)
precnumber = "" .. number .. "." .. string.rep("0", prec)
end
end
number = precnumber .. "1"
number = precnumber .. "1"
precA = "true"
precA = "true"
else
number = args[1]
end
end
local digitml = {
if lang == "arabic-indic" then
["0"] = '൦',
lang = "ks"
["1"] = '൧',
["2"] = '൨',
["3"] = '൩',
["4"] = '൪',
["5"] = '൫',
["6"] = '൬',
["7"] = '൭',
["8"] = '൮',
["9"] = '൯'
}
local digitmn = {
["0"] = '᠐',
["1"] = '᠑',
["2"] = '᠒',
["3"] = '᠓',
["4"] = '᠔',
["5"] = '᠕',
["6"] = '᠖',
["7"] = '᠗',
["8"] = '᠘',
["9"] = '᠙'
}
local digitte = {
["0"] = '౦',
["1"] = '౧',
["2"] = '౨',
["3"] = '౩',
["4"] = '౪',
["5"] = '౫',
["6"] = '౬',
["7"] = '౭',
["8"] = '౮',
["9"] = '౯'
}
local digitth = {
["0"] = '๐',
["1"] = '๑',
["2"] = '๒',
["3"] = '๓',
["4"] = '๔',
["5"] = '๕',
["6"] = '๖',
["7"] = '๗',
["8"] = '๘',
["9"] = '๙'
}
 
if args[2] == "arabic-indic" then
language = "ks"
elseif args[2] == "ml-old" then
language = "ml"
else
language = args[2]
end
end
-- Formatnum
-- Formatnum
if tonumber(number) ~= nil and mw.language.isKnownLanguageTag(language or "") == true then
if tonumber(number) ~= nil and mw.language.isKnownLanguageTag(lang) == true then
formatnum = mw.getLanguage( language ):formatNum( tonumber(number) )
formatnum = mw.getLanguage( lang ):formatNum( tonumber(number) )
elseif tonumber(number) ~= nil then
formatnum = mw.getLanguage(frame:preprocess( "{{int:lang}}" )):formatNum(tonumber(number))
else
else
formatnum = number
formatnum = number
Line 100: Line 60:
-- Formatnum, special cases
-- Formatnum, special cases
local numsc = formatnum
local numsc = formatnum
if args[2] == "ml-old" then
if lang == "ml-old" or lang == "mn" or lang == "te" or lang == "th" then
for en, ml in pairs(digitml) do
for i, v in ipairs( digit[lang] ) do
numsc = mw.ustring.gsub(numsc, en, ml)
numsc = mw.ustring.gsub(numsc, tostring(i-1), v)
end
elseif args[2] == "mn" then
for en, mn in pairs(digitmn) do
numsc = mw.ustring.gsub(numsc, en, mn)
end
elseif args[2] == "te" then
for en, te in pairs(digitte) do
numsc = mw.ustring.gsub(numsc, en, te)
end
elseif args[2] == "th" then
for en, th in pairs(digitth) do
numsc = mw.ustring.gsub(numsc, en, th)
end
end
end
end
Line 126: Line 74:
-- separator
-- separator
if args.sep ~= nil and args.sep ~= "" and formatA ~= "true" then
if sep ~= "" and formatA ~= "true" then
local replace = mw.ustring.gsub( "", "%%", "%%%%" )
local replace = mw.ustring.gsub( "", "%%", "%%%%" )
if mw.language.isKnownLanguageTag(language or "") == true then
local separator = mw.ustring.sub(mw.getLanguage(lang):formatNum(tonumber("10000")), 3, 3)
local separator = mw.ustring.sub(mw.getLanguage(language):formatNum(tonumber("10000")), 3, 3)
if tostring(mw.ustring.find(separator, "%s")) == "1" then
if tostring(mw.ustring.find(separator, "%s")) == "1" then
pattern = mw.ustring.format(separator, "%s")
pattern = mw.ustring.format(separator, "%s")
elseif tostring(mw.ustring.find(separator, "%p")) == "1" then
elseif tostring(mw.ustring.find(separator, "%p")) == "1" then
pattern = mw.ustring.gsub(separator, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1")
pattern = mw.ustring.gsub(separator, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1")
else
pattern = ""
end
return tostring(mw.ustring.gsub( postprec, pattern, replace ))
else
else
local separator = mw.ustring.sub(mw.getLanguage(frame:preprocess("{{int:lang}}")):formatNum(tonumber("10000")), 3, 3)
pattern = ""
if tostring(mw.ustring.find(separator, "%s")) == "1" then
pattern = mw.ustring.format(separator, "%s")
elseif tostring(mw.ustring.find(separator, "%p")) == "1" then
pattern = mw.ustring.gsub(separator, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1")
else
pattern = ""
end
return tostring(mw.ustring.gsub( postprec, pattern, replace ))
end
end
return tostring(mw.ustring.gsub( postprec, pattern, replace ))
else
else
return postprec
return postprec

Revision as of 14:43, 29 December 2014

Module documentation[ create · purge ]
This documentation is transcluded from Module:Formatnum/doc.
-- This module is intended to replace the functionality of Template:Formatnum and related templates.
local p = {}

local digit = { -- languages not supported by the Lua of wikimedia software
	["ml-old"] = {	'൦', '൧', '൨', '൩', '൪', '൫', '൬', '൭', '൮', '൯' },
	["mn"]     = {  '᠐', '᠑', '᠒', '᠓', '᠔', '᠕', '᠖', '᠗', '᠘', '᠙'},
	["te"]     = { '౦', '౧',	 '౨', '౩', '౪', '౫', '౬', '౭', '౮', '౯'},
	["th"]     = { '๐', '๑', '๒', '๓', '๔',	 '๕', '๖', '๗',	 '๘', '๙'}
}
	
function p.main(frame)
	local pframe = frame:getParent()
	local config = frame.args
	local prec   = pframe.args.prec
	local number = pframe.args[1]
	local lang   = string.lower(pframe.args[2])
	local sep    = pframe.args.sep or ""
	if not lang or not mw.language.isSupportedLanguage(lang) then 
		lang = frame:preprocess( "{{int:lang}}" )
	end
	return p._main(number, lang, sep, prec, config)
end	
	
function p._main(number, lang, sep, prec, config)
	-- Preprocessing prec
	local precNum = tonumber(prec)
	if precNum ~= nil and precNum > 0 and tonumber(number) ~= nil then
		if string.find(tostring(number), "%.") ~= nil then
			if (string.len(number) - string.find(tostring(number), "%.")) < precNum then
				precnumber = "" .. number .. string.rep("0", precNum - (string.len(number) - string.find(tostring(number), "%.")))
			elseif (string.len(number) - string.find(tostring(number), "%.")) > precNum then
				local proc = string.gsub(number, "0+$", "")
				if (string.len(proc) - string.find(tostring(proc), "%.")) < precNum then
					precnumber = proc .. string.rep("0", precNum - (string.len(proc) - string.find(tostring(proc), "%.")))
				else
					precnumber = proc
				end
			elseif (string.len(number) - string.find(tostring(number), "%.")) == precNum then
				precnumber = number
			end
		else
			precnumber = "" .. number .. "." .. string.rep("0", prec)
		end
		number = precnumber .. "1"
		precA = "true"
	end
	
	if lang == "arabic-indic" then
		lang = "ks"
	end
		
	-- Formatnum
	if tonumber(number) ~= nil and mw.language.isKnownLanguageTag(lang) == true then
		formatnum = mw.getLanguage( lang ):formatNum( tonumber(number) )
	else
		formatnum = number
		formatA = "true"
	end
	
	-- Formatnum, special cases
	local numsc = formatnum
	if lang == "ml-old" or lang == "mn" or lang == "te" or lang == "th" then
		for i, v in ipairs( digit[lang] ) do
			numsc = mw.ustring.gsub(numsc, tostring(i-1), v)
		end
	end
	
	--Postprocessing prec
	if precA == "true" then
		postprec = mw.ustring.sub(numsc, 1, (mw.ustring.len(numsc) - 1))
	else
		postprec = numsc
	end
	
	-- separator
	if sep ~= "" and formatA ~= "true" then
		local replace = mw.ustring.gsub( "", "%%", "%%%%" )
		local separator = mw.ustring.sub(mw.getLanguage(lang):formatNum(tonumber("10000")), 3, 3)
		if tostring(mw.ustring.find(separator, "%s")) == "1" then
			pattern = mw.ustring.format(separator, "%s")
		elseif tostring(mw.ustring.find(separator, "%p")) == "1" then
			pattern = mw.ustring.gsub(separator, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1")
		else
			pattern = ""
		end
		return tostring(mw.ustring.gsub( postprec, pattern, replace ))
	else
		return postprec
	end
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.