A World of Warcraft Experience Bar addon
worldofwarcraft
wow
addon
midnight
1local _, F = ...
2
3F.Utils = {}
4
5function F.Utils.Round(num, decimals)
6 local mult = 10^(decimals or 0)
7 return math.floor(num * mult + 0.5) / mult
8end
9
10-- Format large numbers into readable strings (commas or K/M suffixes)
11function F.Utils.FormatLargeNumber(n)
12 n = tonumber(n) or 0
13
14 if n >= 1000000 then
15 return F.Utils.Round(n / 1000000, 1) .. "M"
16 elseif n >= 1000 then
17 -- Insert thousands separators (commas)
18 local s = tostring(math.floor(n))
19 local k
20 repeat
21 s, k = s:gsub("^(-?%d+)(%d%d%d)", "%1,%2")
22 until k == 0
23 return s
24 else
25 return tostring(n)
26 end
27end
28
29FormatLargeNumber = function(n) return F.Utils.FormatLargeNumber(n) end
30
31function F.Utils.FormatTime(time, formatString)
32 if time <= 59 then return "< 1m" end
33
34 local d = math.floor(time / 86400)
35 local h = math.floor((time % 86400) / 3600)
36 local m = math.floor((time % 3600) / 60)
37 local s = time % 60
38
39 local t = formatString or "%dd %hh %mm"
40
41 local pad = function(v) return v < 10 and "0" .. v or v end
42
43 local subs = {
44 ["%%D([Dd]?)"] = d > 0 and (pad(d) .. "%1") or "",
45 ["%%d([Dd]?)"] = d > 0 and (d .. "%1") or "",
46 ["%%H([Hh]?)"] = (d > 0 or h > 0) and (pad(h) .. "%1") or "",
47 ["%%h([Hh]?)"] = (d > 0 or h > 0) and (h .. "%1") or "",
48 ["%%M([Mm]?)"] = pad(m) .. "%1",
49 ["%%m([Mm]?)"] = m .. "%1",
50 }
51
52 for k,v in pairs(subs) do
53 t = t:gsub(k, v)
54 end
55
56 return strtrim(t:gsub("^%s*0*", ""):gsub("^%s*[DdHhMm]", ""), " :/-|")
57end