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 13: | Line 13: | ||
[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" | ||
} | } | ||
-- | -- | ||
-- | -- Rate calculation accessed through #invoke | ||
-- | -- | ||
-- @param | -- @param frame {table} | ||
-- @return {number} | -- @return {number} | ||
-- | -- | ||
function p.calcRate(frame) | |||
-- Separate the two numbers passed in as strings | |||
local mult = {} | local mult = {} | ||
mult[1] = | mult[1] = frame.args[1] | ||
mult[2] = | mult[2] = frame.args[2] | ||
-- Initialize numbers for string conversion | -- Initialize numbers for string conversion | ||
local frac = {} | local frac = {} | ||
Line 48: | Line 46: | ||
end | end | ||
end | end | ||
-- These probably don't need to be redefined, but I'm still super new to Lua so I'll come back to it later xd | |||
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 | end | ||
Line 61: | Line 64: | ||
local function fmtRate(rate, fmt, digits) | local function fmtRate(rate, fmt, digits) | ||
local out = "" | local out = "" | ||
-- Don't want nil comparisons or whatever | |||
if digits == nil then | |||
digits = 0 | |||
end | |||
local frac = {} | local frac = {} | ||
-- If either number is a fraction, split it up into numerator and denominator | -- If either number is a fraction, split it up into numerator and denominator | ||
Line 77: | Line 84: | ||
if fmt == "f" then | if fmt == "f" then | ||
if wholeFractions[rate] == nil then | if wholeFractions[rate] == nil then | ||
-- Set maximum number of decimal places | -- Set maximum number of decimal places | ||
if | if rate < 10 then | ||
-- Displays with two places between 1.01 ~ 9.99 | -- Displays with two places between 1.01 ~ 9.99 | ||
digits = math.max(digits, 2) | digits = math.max(digits, 2) | ||
elseif | elseif rate < 100 then | ||
-- Displays with one place between 10.1 ~ 99.9 | -- Displays with one place between 10.1 ~ 99.9 | ||
digits = math.max(digits, 1) | digits = math.max(digits, 1) | ||
else | |||
-- Otherwise just displays the whole number at 100+ | |||
digits = math.max(digits, 0) | |||
end | end | ||
-- Even if we're displaying one or more decimal places, cut zeroes off | -- Even if we're displaying one or more decimal places, cut zeroes off | ||
out = string.format("1/%." .. digits .. "f", 1/rate) | out = out .. string.format("%s", string.format("1/%." .. digits .. "f", 1/rate)):gsub("%.?0+$", "") | ||
else | else | ||
out = wholeFractions[rate] | out = out .. wholeFractions[rate] | ||
end | end | ||
return out | return out | ||
elseif fmt == "p" then | elseif fmt == "p" then | ||
-- Display up to five decimal places. Cut zeroes off | -- Display up to five decimal places. Cut zeroes off | ||
rate = string.format("%s%%", string.format("%f", string.format("%.5f", rate*100)):gsub("%.?0+$", "")) | |||
out = out .. rate | |||
return out | |||
return | |||
end | end | ||
end | end | ||
-- | -- | ||
-- Rate display accessed through #invoke | -- Rate display accessed through #invoke | ||
-- | -- | ||
-- @param frame {table} | -- @param frame {table} | ||
Line 112: | Line 116: | ||
-- | -- | ||
function p.displayRate(frame) | function p.displayRate(frame) | ||
-- Separate the | -- Separate the rate (number passed in as string) from the format (string) and digits (string). digits parameter is optional | ||
local | local rate = frame.args[1] | ||
local | local fmt = frame.args[2] | ||
local digits = frame.args[3] | |||
local out = "" | local out = "" | ||
if fmt == "f" then | if fmt == "f" then | ||
-- Fraction | -- Fraction | ||
out = out .. fmtRate(rate, "f", digits) | |||
elseif fmt == "p" then | elseif fmt == "p" then | ||
-- Percentage | -- Percentage | ||
out = out .. fmtRate(rate | out = out .. fmtRate(rate, "p") | ||
elseif fmt == "n" then | elseif fmt == "n" then | ||
-- | -- Fraction (Percentage) | ||
out = out .. fmtRate(rate, "f", 2) .. " (" .. fmtRate(rate, "p") .. ")" | |||
end | end | ||
return out | return out |