Module:Decimal-align: Difference between revisions
Appearance
Content deleted Content added
No edit summary |
|||
Line 5: | Line 5: | ||
local input_string_raw = frame.args[1] |
local input_string_raw = frame.args[1] |
||
string = frame:preprocess( input_string_raw ) |
string = frame:preprocess( input_string_raw ) |
||
local left_percent = tonumber(frame.args['left']) |
|||
-- local left_percent = tonumber(20) |
|||
if not left_percent then |
|||
left_percent = 50 |
|||
end |
|||
right_percent = 100 - left_percent |
|||
for number in mw.ustring.gmatch( string, '%|[%d%.,]+' ) do |
for number in mw.ustring.gmatch( string, '%|[%d%.,]+' ) do |
||
Line 10: | Line 17: | ||
right = mw.ustring.match(number, '%.[%d ]*$') |
right = mw.ustring.match(number, '%.[%d ]*$') |
||
if left=='' then left='0' end |
if left=='' then left='0' end |
||
formatted_number = '%|<span style="float: left; text-align: right; width: |
formatted_number = '%|<span style=\"float: left; text-align: right; width: ' .. tostring(left_percent) .. '%;\">' .. left .. '</span>' |
||
if right then |
if right then |
||
formatted_number = formatted_number .. '<span style="float: right; text-align: left; width: |
formatted_number = formatted_number .. '<span style=\"float: right; text-align: left; width: ' .. tostring(right_percent) .. '%;\">' .. right .. '</span>' |
||
end |
end |
||
string = mw.ustring.gsub( string, number, formatted_number ) |
string = mw.ustring.gsub( string, number, formatted_number ) |
Revision as of 22:04, 2 January 2019
--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 )
local left_percent = tonumber(frame.args['left'])
-- local left_percent = tonumber(20)
if not left_percent then
left_percent = 50
end
right_percent = 100 - left_percent
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
formatted_number = '%|<span style=\"float: left; text-align: right; width: ' .. tostring(left_percent) .. '%;\">' .. left .. '</span>'
if right then
formatted_number = formatted_number .. '<span style=\"float: right; text-align: left; width: ' .. tostring(right_percent) .. '%;\">' .. right .. '</span>'
end
string = mw.ustring.gsub( string, number, formatted_number )
end
return string
end
return p