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
Created page with '-- This module is intended to replace the functionality of Template:Formatnum and related templates. -- This module is in beta. Use with caution. local p = {}...'
 
No edit summary
Line 8: Line 8:
local args = pframe.args
local args = pframe.args
-- Preprocessing prec
if tonumber(args.prec) ~= nil and tonumber(args.prec) > 0 and tonumber(args[1]) ~= nil then
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.find(tostring(args[1]), "%.") ~= nil then
precnumber = string.sub(args[1], 1, (string.find(tostring(args[1]), "%.") + args.prec))
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
else
precnumber = "" .. args[1] .. "." .. string.rep("0", args.prec)
precnumber = "" .. args[1] .. "." .. string.rep("0", args.prec)
end
end
number = "" .. precnumber .. "1"
number = precnumber .. "1"
precA = "true"
precA = "true"
else
else
number = args[1]
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
end
if tonumber(number) ~= nil and mw.language.isSupportedLanguage(args[2] or "") == true then
-- Formatnum
formatnum = mw.getLanguage( args[2] ):formatNum( tonumber(number) )
if tonumber(number) ~= nil and mw.language.isKnownLanguageTag(language or "") == true then
formatnum = mw.getLanguage( language ):formatNum( tonumber(number) )
elseif tonumber(number) ~= nil then
elseif tonumber(number) ~= nil then
formatnum = mw.getLanguage(frame:preprocess( "{{int:lang}}" )):formatNum(tonumber(number))
formatnum = mw.getLanguage(frame:preprocess( "{{int:lang}}" )):formatNum(tonumber(number))
Line 28: Line 98:
end
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
if precA == "true" then
postprec = string.sub(formatnum, 1, (string.len(formatnum) - 1))
postprec = mw.ustring.sub(numsc, 1, (mw.ustring.len(numsc) - 1))
else
else
postprec = formatnum
postprec = numsc
end
end
-- Eqvaluent functions to Template:FormatnumSigns
-- Seperator
-- Table over what thousands seperator is used.
if args.sep ~= nil and args.sep ~= "" then
thousand = {
ckb="٫",
fa="٫",
glk="٫",
ks="٫",
mzn="٫",
ur=",",
ca=".",
da=",",
de=",",
gl=",",
mk=",",
nds=",",
nl=",",
sl=",",
cs=",",
fr=",",
hu=",",
ro=",",
pl=",",
nb=",",
nn=",",
no=",",
sv=",",
bn=".",
ml=".",
hi=".",
sa=".",
ta=".",
kn=".",
te=".",
mr=".",
["or"]=".",
pa=".",
gu=".",
bho=".",
as="."
}
-- Table ends
if args.sep ~= nil then
local replace = mw.ustring.gsub( "", "%%", "%%%%" )
local replace = mw.ustring.gsub( "", "%%", "%%%%" )
local pattern = mw.ustring.gsub( (thousand[args[2] or frame:preprocess( "{{int:lang}}" )] or "."), "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" )
if mw.language.isKnownLanguageTag(language or "") == true then
return tostring(mw.ustring.gsub( postprec, pattern, replace ))
local seperator = mw.ustring.sub(mw.getLanguage(language):formatNum(tonumber("1000")), 2, 2)
if tostring(mw.ustring.find(seperator, "%s")) == "1" then
pattern = mw.ustring.format(" ", "%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("1000")), 2, 2)
if tostring(mw.ustring.find(seperator, "%s")) == "1" then
pattern = mw.ustring.format(" ", "%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
else
return postprec
return postprec

Revision as of 18:04, 6 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 = ""
	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 ~= "" then
		local replace = mw.ustring.gsub( "", "%%", "%%%%" )
		if mw.language.isKnownLanguageTag(language or "") == true then
			local seperator = mw.ustring.sub(mw.getLanguage(language):formatNum(tonumber("1000")), 2, 2)
			if tostring(mw.ustring.find(seperator, "%s")) == "1" then
				pattern = mw.ustring.format(" ", "%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("1000")), 2, 2)
			if tostring(mw.ustring.find(seperator, "%s")) == "1" then
				pattern = mw.ustring.format(" ", "%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.