FANDOM


local FormatNum = {}
local language = mw.language.new('en')
 
-- from sam_lie
-- add comma to separate thousands
function FormatNum.commavalue(amount)
  local formatted = amount
  while true do  
    formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
    if (k==0) then
      break
    end
  end
  return formatted
end
 
-- given a numeric value formats output with comma to separate thousands
-- and rounded to given decimal places
function FormatNum.formatNum(amount, decimal, prefix, neg_prefix)
  local str_amount,  formatted, famount, remain
 
  decimal = decimal or 2  -- default 2 decimal places
  neg_prefix = neg_prefix or "-" -- default negative sign
 
  famount = math.abs(round(amount,decimal))
  famount = math.floor(famount)
 
  remain = round(math.abs(amount) - famount, decimal)
 
        -- comma to separate the thousands
  formatted = comma_value(famount)
 
        -- attach the decimal portion
  if (decimal > 0) then
    remain = string.sub(tostring(remain),3)
    formatted = formatted .. "." .. remain ..
                string.rep("0", decimal - string.len(remain))
  end
 
        -- attach prefix string e.g '$' 
  formatted = (prefix or "") .. formatted 
 
        -- if value is negative then format accordingly
  if (amount<0) then
    if (neg_prefix=="()") then
      formatted = "("..formatted ..")"
    else
      formatted = neg_prefix .. formatted 
    end
  end
 
  return formatted
end
 
return FormatNum

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.