モジュール:Formatnum

提供:脳科学辞典
2014年4月20日 (日) 10:36時点におけるWikiSysop (トーク | 投稿記録)による版 (1版)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
ナビゲーションに移動 検索に移動

このモジュールについての説明文ページを モジュール:Formatnum/doc に作成できます

-- This module is intended to replace the functionality of Template:Formatnum and related templates.
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
	
	-- separator
	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 separator = mw.ustring.sub(mw.getLanguage(language):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
			local separator = mw.ustring.sub(mw.getLanguage(frame:preprocess("{{int: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 ))
		end
	else
		return postprec
	end
end

return p