Anonymous

Module:Shared: Difference between revisions

From Melvor Idle
Fix name clash between parameters and module name
m (Add functions for parsing numbers)
Tag: Reverted
(Fix name clash between parameters and module name)
(2 intermediate revisions by 2 users not shown)
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 numModule = 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 165: Line 169:
--Adds commas
--Adds commas
function p.formatnum(number)
function p.formatnum(number)
if tonumber(number) == nil then
return numModule.formatnum(number)
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 187: Line 178:


function p.round(val, maxDigits, minDigits)
function p.round(val, maxDigits, minDigits)
if val == nil then
return numModule.round(val, maxDigits, minDigits)
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)
local mult = 10^(numDecimalPlaces or 0)
return numModule.round2(num, numDecimalPlaces)
return math.floor(num * mult + 0.5) / mult
end
end


Line 310: Line 277:
-- Euclidean Greatest Common Divisor algorithm
-- Euclidean Greatest Common Divisor algorithm
function p.gcd(a, b)
function p.gcd(a, b)
if b ~= 0 then
return numModule.gcd(a, b)
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)
local gcd = p.gcd(n, d)
return numModule.fraction(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)
local gcd = p.gcd(n, d)
return numModule.fractionpair(n, d)
return n / gcd, d / gcd
end
end


Line 406: Line 367:
--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)
if number >= 0 then
return numModule.numStrWithSign(number)
return '+'..p.formatnum(number)
else
return p.formatnum(number)
end
end
end


Line 456: Line 413:
end
end
return namespace, localID
return namespace, localID
end
-- Converts a string to a numeric value, or the default value if the
-- string isn't a number.
function p.toNumberOrDefault(str, def)
local num = tonumber(str)
if num then
return num
else
return def
end
end
-- Attempts to convert a string to a numeric value.
-- Throws an error if the value isn't a number.
function p.toNumberOrError(str, errorMsg)
local num = tonumber(str)
local msg = errorMsg
if msg == nil then
msg = "NaN"
end
if num then
return num
else
error(msg)
end
end
end


return p
return p
913

edits