Jump to content

Module:Decimal-align

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Swpb (talk | contribs) at 21:10, 26 December 2018 (Created page with '--Align numbers in table cells on the decimal point local p = {} function p.main(frame) local input_string_raw = frame.args[1] string = frame:preprocess( inp...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

--Align numbers in table cells on the decimal point	
local p = {}

function p.main(frame)
	local input_string_raw = frame.args[1]
	string = frame:preprocess( input_string_raw )
	
	for number in mw.ustring.gmatch( string, '%|[%d%.,]+' ) do
		left = mw.ustring.sub(mw.ustring.match(number, '^%|[%d, ]*'),2)
		right = mw.ustring.match(number, '%.[%d ]*$')
		if left=='' then left='0' end
		if not right then
			formatted_number = '%|<span style="float: left; text-align: right; width: 50%;">' .. left .. '</span>'
		else
			formatted_number = '%|<span style="float: left; text-align: right; width: 50%;">' .. left .. '</span><span style="float: right; text-align: left; width: 50%;">' .. right .. '</span>'
		end
		string = mw.ustring.gsub( string, number, formatted_number )
	end

	return string
	end

return p