Editing Module:Rates
From Ephinea PSO Wiki
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local wholeFractions = | local wholeFractions = | ||
{ | { | ||
Line 13: | Line 7: | ||
[0.875] = "7/8", | [0.875] = "7/8", | ||
[0.85] = "17/20", | [0.85] = "17/20", | ||
[0.8] = "4/5", | [0.8] = "4/5", | ||
[0.6] = "3/5", | [0.6] = "3/5", | ||
[0.5625] = "9/16", | [0.5625] = "9/16", | ||
[0.55] = "9/20", | [0.55] = "9/20", | ||
[0.5] = "1/2" | [0.5] = "1/2" | ||
} | } | ||
function p.calcRate(frame) | |||
return frame.args[1]*frame.args[2] | |||
end | end | ||
local function fmtRate(rate, fmt, digits) | local function fmtRate(rate, fmt, digits) | ||
local out = "" | local out = "" | ||
if digits == nil then | |||
digits = 0 | |||
end | |||
local frac = {} | local frac = {} | ||
for number in string.gmatch(rate, '([^/]+)') do | for number in string.gmatch(rate, '([^/]+)') do | ||
table.insert(frac, number) | table.insert(frac, number) | ||
-- first time here is num, second time is den | |||
-- third and beyond invalid | |||
end | end | ||
if frac[2] == nil then | if frac[2] == nil then | ||
frac[2] = 1 | frac[2] = 1 | ||
Line 73: | Line 34: | ||
local num = frac[1] | local num = frac[1] | ||
local den = frac[2] | local den = frac[2] | ||
rate = num / den | rate = num / den | ||
if fmt == "f" then | if fmt == "f" then | ||
if wholeFractions[rate] == nil then | if wholeFractions[rate] == nil then | ||
if | if rate < 10 then | ||
digits = math.max(digits, 2) | digits = math.max(digits, 2) | ||
elseif | elseif rate < 100 then | ||
digits = math.max(digits, 1) | digits = math.max(digits, 1) | ||
else | |||
digits = math.max(digits, 0) | |||
end | end | ||
out = out .. string.format("%s", string.format("1/%." .. digits .. "f", 1/rate)):gsub("%.?0+$", "") | |||
out = string.format("1/%." .. digits .. "f", 1/rate) | |||
else | else | ||
out = wholeFractions[rate] | out = out .. wholeFractions[rate] | ||
end | end | ||
return out | return out | ||
elseif fmt == "p" then | elseif fmt == "p" then | ||
rate = string.format("%s%%", string.format("%f", string.format("%.5f", rate*100)):gsub("%.?0+$", "")) | |||
out = out .. rate | |||
return out | |||
return | |||
end | end | ||
end | end | ||
function p.displayRate(frame) | function p.displayRate(frame) | ||
local rate = frame.args[1] | |||
local | local fmt = frame.args[2] | ||
local | local digits = frame.args[3] | ||
local out = "" | local out = "" | ||
if fmt == "f" then | if fmt == "f" then | ||
out = out .. fmtRate(rate, "f", digits) | |||
elseif fmt == "p" then | elseif fmt == "p" then | ||
out = out .. fmtRate(rate, "p") | |||
out = out .. fmtRate(rate | |||
elseif fmt == "n" then | elseif fmt == "n" then | ||
out = out .. fmtRate(rate, "f", 2) .. " (" .. fmtRate(rate, "p") .. ")" | |||
end | end | ||
return out | return out | ||
Line 146: | Line 72: | ||
return p | return p | ||