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 minor fix
mNo edit summary
Line 132: Line 132:
local seperator = mw.ustring.sub(mw.getLanguage(language):formatNum(tonumber("10000")), 3, 3)
local seperator = mw.ustring.sub(mw.getLanguage(language):formatNum(tonumber("10000")), 3, 3)
if tostring(mw.ustring.find(seperator, "%s")) == "1" then
if tostring(mw.ustring.find(seperator, "%s")) == "1" then
pattern = mw.ustring.format(" ", "%s")
pattern = mw.ustring.format(seperator, "%s")
elseif tostring(mw.ustring.find(seperator, "%p")) == "1" then
elseif tostring(mw.ustring.find(seperator, "%p")) == "1" then
pattern = mw.ustring.gsub(seperator, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1")
pattern = mw.ustring.gsub(seperator, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1")
Line 142: Line 142:
local seperator = mw.ustring.sub(mw.getLanguage(frame:preprocess("{{int:lang}}")):formatNum(tonumber("10000")), 3, 3)
local seperator = mw.ustring.sub(mw.getLanguage(frame:preprocess("{{int:lang}}")):formatNum(tonumber("10000")), 3, 3)
if tostring(mw.ustring.find(seperator, "%s")) == "1" then
if tostring(mw.ustring.find(seperator, "%s")) == "1" then
pattern = mw.ustring.format(" ", "%s")
pattern = mw.ustring.format(seperator, "%s")
elseif tostring(mw.ustring.find(seperator, "%p")) == "1" then
elseif tostring(mw.ustring.find(seperator, "%p")) == "1" then
pattern = mw.ustring.gsub(seperator, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1")
pattern = mw.ustring.gsub(seperator, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1")

Revision as of 21:41, 11 January 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.
-- This module is in beta. Use with caution.
local p = {}

function p.main(frame)
	local pframe = frame:getParent()
	local config = frame.args
	local args = pframe.args
	
	-- Preprocessing prec
	if tonumber(args.prec) ~= nil and tonumber(args.prec) > 0 and tonumber(args[1]) ~= nil then
		if string.find(tostring(args[1]), "%.") ~= nil then
			if (string.len(args[1]) - string.find(tostring(args[1]), "%.")) < tonumber(args.prec) then
				precnumber = "" .. args[1] .. string.rep("0", tonumber(args.prec) - (string.len(args[1]) - string.find(tostring(args[1]), "%.")))
			elseif (string.len(args[1]) - string.find(tostring(args[1]), "%.")) > tonumber(args.prec) then
				local proc = string.gsub(args[1], "0+$", "")
				if (string.len(proc) - string.find(tostring(proc), "%.")) < tonumber(args.prec) then
					precnumber = proc .. string.rep("0", tonumber(args.prec) - (string.len(proc) - string.find(tostring(proc), "%.")))
				else
					precnumber = proc
				end
			elseif (string.len(args[1]) - string.find(tostring(args[1]), "%.")) == tonumber(args.prec) then
				precnumber = args[1]
			end
		else
			precnumber = "" .. args[1] .. "." .. string.rep("0", args.prec)
		end
		number = precnumber .. "1"
		precA = "true"
	else
		number = args[1]
	end
	
	local digitml = {
	["0"] = '൦',
	["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
		
	-- Formatnum
	if tonumber(number) ~= nil and mw.language.isKnownLanguageTag(language or "") == true then
		formatnum = mw.getLanguage( language ):formatNum( tonumber(number) )
	elseif tonumber(number) ~= nil then
		formatnum = mw.getLanguage(frame:preprocess( "{{int:lang}}" )):formatNum(tonumber(number))
	else
		formatnum = number
		formatA = "true"
	end
	
	-- Formatnum, special cases
	local numsc = formatnum
	if args[2] == "ml-old" then
		for en, ml in pairs(digitml) do
			numsc = mw.ustring.gsub(numsc, en, ml)
		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
	
	--Postprocessing prec
	if precA == "true" then
		postprec = mw.ustring.sub(numsc, 1, (mw.ustring.len(numsc) - 1))
	else
		postprec = numsc
	end
	
	-- Seperator
	if args.sep ~= nil and args.sep ~= "" and formatA ~= "true" then
		local replace = mw.ustring.gsub( "", "%%", "%%%%" )
		if mw.language.isKnownLanguageTag(language or "") == true then
			local seperator = mw.ustring.sub(mw.getLanguage(language):formatNum(tonumber("10000")), 3, 3)
			if tostring(mw.ustring.find(seperator, "%s")) == "1" then
				pattern = mw.ustring.format(seperator, "%s")
			elseif tostring(mw.ustring.find(seperator, "%p")) == "1" then
				pattern = mw.ustring.gsub(seperator, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1")
			else
				pattern = ""
			end
			return tostring(mw.ustring.gsub( postprec, pattern, replace ))
		else
			local seperator = mw.ustring.sub(mw.getLanguage(frame:preprocess("{{int:lang}}")):formatNum(tonumber("10000")), 3, 3)
			if tostring(mw.ustring.find(seperator, "%s")) == "1" then
				pattern = mw.ustring.format(seperator, "%s")
			elseif tostring(mw.ustring.find(seperator, "%p")) == "1" then
				pattern = mw.ustring.gsub(seperator, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1")
			else
				pattern = ""
			end
			return tostring(mw.ustring.gsub( postprec, pattern, replace ))
		end
	else
		return postprec
	end
end

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