Module:CommonFunctions: Difference between revisions
From Elwiki
No edit summary |
No edit summary |
||
Line 30: | Line 30: | ||
local mult = 10^(numDecimalPlaces or 0) | local mult = 10^(numDecimalPlaces or 0) | ||
return math.floor(num * mult + 0.5) / mult | return math.floor(num * mult + 0.5) / mult | ||
end | |||
-- Implement string trim. | |||
function trim(s) | |||
return (string.gsub(s, "^%s*(.-)%s*$", "%1")) | |||
end | end | ||
-- Implement splitting string to a table. | -- Implement splitting string to a table. | ||
function split ( | function split(pString, pPattern) | ||
if pPattern == nil then pPattern = "," end | |||
local Table = {} | |||
local fpat = "(.-)" .. pPattern | |||
local last_end = 1 | |||
local s, e, cap = pString:find(fpat, 1) | |||
while s do | |||
if s ~= 1 or cap ~= "" then | |||
table.insert(Table,cap) | |||
end | |||
last_end = e+1 | |||
s, e, cap = pString:find(fpat, last_end) | |||
end | |||
if last_end <= #pString then | |||
cap = trim(pString:sub(last_end)) | |||
table.insert(Table, trim(cap)) | |||
end | |||
return Table | |||
end | end |