Module:Rates: Difference between revisions

From Ephinea PSO Wiki
m (Undo revision 17170 by Ryan (talk))
Tag: Undo
No edit summary
Line 18: Line 18:
end
end


local function trimRate(rate, fmt, digits)
local function fmtRate(rate, fmt, digits)
   local out = ""
   local out = ""
   if digits == nil then
   if digits == nil then
Line 24: Line 24:
   end
   end
   if fmt == "f" then
   if fmt == "f" then
     if rate < 10 then
     if wholeFractions[rate] == nil then
      digits = math.max(digits, 2)
      if rate < 10 then
    elseif rate < 100 then
        digits = math.max(digits, 2)
       digits = math.max(digits, 1)
      elseif rate < 100 then
        digits = math.max(digits, 1)
       else
        digits = math.max(digits, 0)
      end
      out = out .. string.format("%f", string.format(" %." .. digits .. "f", rate)):gsub("%.?0+$", "")
     else
     else
       digits = math.max(digits, 0)
       out = out .. wholeFractions[rate]
     end
     end
    out = out .. string.format("%f", string.format(" %." .. digits .. "f", rate)):gsub("%.?0+$", "")
     return out
     return out
   elseif fmt == "p" then
   elseif fmt == "p" then
     rate = string.format("%f", string.format(" %.5f", rate)):gsub("%.?0+$", "")
     rate = string.format("%s%%", string.format("%f", string.format(" %.5f", rate)):gsub("%.?0+$", ""))
     out = out .. rate
     out = out .. rate
     return out
     return out
Line 46: Line 50:
   local out = ""
   local out = ""
   if fmt == "f" then
   if fmt == "f" then
    if wholeFractions[rate] == nil then
  out = out .. fmtRate(1/rate, "f", digits)
      out = out .. string.format("1/%s", trimRate(1/rate, "f", digits))
   elseif fmt == "p" then
    else
     out = out .. fmtRate(rate*100, "p")
      out = out .. wholeFractions[rate]
   elseif fmt == "n" then
    end
     out = out .. fmtRate(rate, "f", 2) .. " (" .. fmtRate(rate, "p") .. ")"
  end
   if fmt == "p" then
     out = out .. string.format("%s%%", trimRate(rate*100, "p"))
   end
  if fmt == "n" then
     out = out .. p.displayRate(rate, "f", 2) .. " (" .. p.displayRate(rate, "p") .. ")"
   end
   end
   return out
   return out

Revision as of 21:46, 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)
  return frame.args[1]*frame.args[2]
end

local function fmtRate(rate, fmt, digits)
  local out = ""
  if digits == nil then
    digits = 0
  end
  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("%f", string.format(" %." .. digits .. "f", 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)):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(1/rate, "f", digits)
  elseif fmt == "p" then
    out = out .. fmtRate(rate*100, "p")
  elseif fmt == "n" then
    out = out .. fmtRate(rate, "f", 2) .. " (" .. fmtRate(rate, "p") .. ")"
  end
  return out
end

return p