Module:Rates: Difference between revisions
From Ephinea PSO Wiki
mNo edit summary |
mNo edit summary |
||
Line 19: | Line 19: | ||
mult[2] = frame.args[2] | mult[2] = frame.args[2] | ||
local frac = {} | local frac = {} | ||
frac[1] | frac[1] = {} | ||
frac[2] = {} | |||
for i=1,2,1 do | for i=1,2,1 do | ||
for number in string.gmatch(mult[i], '([^/]+)') do | for number in string.gmatch(mult[i], '([^/]+)') do | ||
table.insert(frac[i], number) | table.insert(frac[i], number) | ||
end | |||
if frac[i][2] == nil then | |||
frac[i][2] = 1 | |||
end | end | ||
end | end |
Revision as of 23:35, 30 April 2023
Documentation for this module may be created at Module:Rates/doc
local p = {} local wholeFractions = { [0.9] = "9/10", [0.88] = "22/25", [0.875] = "7/8", [0.85] = "17/20", [0.8] = "4/5", [0.6] = "3/5", [0.5625] = "9/16", [0.55] = "9/20", [0.5] = "1/2" } function p.calcRate(frame) local mult = {} mult[1] = frame.args[1] mult[2] = frame.args[2] local frac = {} frac[1] = {} frac[2] = {} for i=1,2,1 do for number in string.gmatch(mult[i], '([^/]+)') do table.insert(frac[i], number) end if frac[i][2] == nil then frac[i][2] = 1 end end local num1 = frac[1][1] local den1 = frac[1][2] local num2 = frac[2][1] local den2 = frac[2][2] return (num1/den1)*(num2/den2) end local function fmtRate(rate, fmt, digits) local out = "" if digits == nil then digits = 0 end local frac = {} for number in string.gmatch(rate, '([^/]+)') do table.insert(frac, number) end if frac[2] == nil then frac[2] = 1 end local num = frac[1] local den = frac[2] rate = num / den if fmt == "f" then if wholeFractions[rate] == nil then if rate < 10 then digits = math.max(digits, 2) elseif rate < 100 then digits = math.max(digits, 1) else digits = math.max(digits, 0) end out = out .. string.format("%s", string.format("1/%." .. digits .. "f", 1/rate)):gsub("%.?0+$", "") else out = out .. wholeFractions[rate] end return out elseif fmt == "p" then rate = string.format("%s%%", string.format("%f", string.format("%.5f", rate*100)):gsub("%.?0+$", "")) out = out .. rate return out end end function p.displayRate(frame) local rate = frame.args[1] local fmt = frame.args[2] local digits = frame.args[3] local out = "" if fmt == "f" then out = out .. fmtRate(rate, "f", digits) elseif fmt == "p" then out = out .. fmtRate(rate, "p") elseif fmt == "n" then out = out .. fmtRate(rate, "f", 2) .. " (" .. fmtRate(rate, "p") .. ")" end return out end return p