12,721
edits
(Remove joinList: Exists within Scribunto provided libraries as mw.text.listToText; Indent with tabs instead of spaces) |
(formatnum: Use faster variant of function) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 123: | Line 123: | ||
function p.tableSort(theTable, sortCol, ascend) | function p.tableSort(theTable, sortCol, ascend) | ||
local new function sorter(r1, r2) | local new function sorter(r1, r2) | ||
if | if ascend then | ||
return r1[sortCol] < r2[sortCol] | return r1[sortCol] < r2[sortCol] | ||
else | else | ||
Line 151: | Line 151: | ||
end | end | ||
--Adds commas | --Adds commas | ||
function p.formatnum(number) | function p.formatnum(number) | ||
if tonumber(number) == nil then | |||
return number | |||
-- | else | ||
local result = number | |||
while true do | |||
-- Format in blocks of 3 digits at a time until formatting is complete | |||
result, k = string.gsub(result, "^(-?%d+)(%d%d%d)", '%1,%2') | |||
if k == 0 then | |||
break | |||
end | |||
end | |||
return result | |||
end | |||
end | end | ||
Line 292: | Line 296: | ||
local gcd = p.gcd(n, d) | local gcd = p.gcd(n, d) | ||
return p.formatnum(n/gcd)..'/'..p.formatnum(d/gcd) | return p.formatnum(n/gcd)..'/'..p.formatnum(d/gcd) | ||
end | |||
--Similar to p.fraction but returns the simplified numerator and denomerator separately without formatting | |||
function p.fractionpair(n, d) | |||
local gcd = p.gcd(n, d) | |||
return n / gcd, d / gcd | |||
end | end | ||