Module:Shared: Difference between revisions

m
Reverted edits by Ricewind (talk) to last revision by Auron956
(Migrate numeric specific functions to Module:Number)
Tag: Reverted
m (Reverted edits by Ricewind (talk) to last revision by Auron956)
Tag: Rollback
Line 1: Line 1:
-- Module that contains all functions regarding numeric formatting and calculations.
-- TODO: Make modules that now call Shared call Number (for relevant functions)
local number = require('Module:Number')
--So there are a handful of functions that I'm using in a lot of places
--So there are a handful of functions that I'm using in a lot of places
--So rather than continue to copy across the same handful of functions to every single new module
--So rather than continue to copy across the same handful of functions to every single new module
Line 169: Line 165:
--Adds commas
--Adds commas
function p.formatnum(number)
function p.formatnum(number)
return number.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
            local k
result, k = string.gsub(result, "^(-?%d+)(%d%d%d)", '%1,%2')
if k == 0 then
break
end
end
return result
end
end
end


Line 178: Line 187:


function p.round(val, maxDigits, minDigits)
function p.round(val, maxDigits, minDigits)
return number.round(val, maxDigits, minDigits)
if val == nil then
return nil
else
if type(maxDigits) == "table" then
minDigits = maxDigits[2]
maxDigits = maxDigits[1]
end
 
local result = val..""
local decimals = string.find(result, "%.")
if decimals ~= nil then
decimals = string.len(result) - decimals
else
decimals = 0
end
 
if maxDigits ~= nil and decimals > maxDigits then
result = string.format("%."..maxDigits.."f", result)
elseif minDigits ~= nil and decimals < minDigits then
result = string.format("%."..minDigits.."f", result)
end
 
return result
end
end
end


--From http://lua-users.org/wiki/SimpleRound
--From http://lua-users.org/wiki/SimpleRound
function p.round2(num, numDecimalPlaces)
function p.round2(num, numDecimalPlaces)
return number.round2(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
end


Line 277: Line 310:
-- Euclidean Greatest Common Divisor algorithm
-- Euclidean Greatest Common Divisor algorithm
function p.gcd(a, b)
function p.gcd(a, b)
return number.gcd(a, b)
if b ~= 0 then
return p.gcd(b, a % b)
else
return math.abs(a)
end
end
end


--Formats a pair of numbers as a reduced fraction
--Formats a pair of numbers as a reduced fraction
function p.fraction(n, d)
function p.fraction(n, d)
return number.fraction(n, d)
local gcd = p.gcd(n, d)
return p.formatnum(n/gcd)..'/'..p.formatnum(d/gcd)
end
end


--Similar to p.fraction but returns the simplified numerator and denomerator separately without formatting
--Similar to p.fraction but returns the simplified numerator and denomerator separately without formatting
function p.fractionpair(n, d)
function p.fractionpair(n, d)
return number.fractionpair(n, d)
local gcd = p.gcd(n, d)
return n / gcd, d / gcd
end
end


Line 367: Line 406:
--Returns a number including the sign, even if positive
--Returns a number including the sign, even if positive
function p.numStrWithSign(number)
function p.numStrWithSign(number)
return number.numStrWithSign(number)
if number >= 0 then
return '+'..p.formatnum(number)
else
return p.formatnum(number)
end
end
end