This commit is contained in:
2025-02-12 23:57:00 +09:00
parent a3fbdd195b
commit aacbfa7c3b

View File

@@ -21,38 +21,54 @@ local font_color = {
} }
local function format_n(amount) local function format_n(amount)
if amount < 0.1 then if math.abs(amount) < 0.009 then
return "0.0" return "0.00"
end end
local suffix = "" local suffix = ""
local suffix_list = { local suffix_list = {
["T"] = 1000000000000, ["T"] = 1e12,
["G"] = 1000000000, ["G"] = 1e9,
["M"] = 1000000, ["M"] = 1e6,
["k"] = 1000, ["k"] = 1e3
} }
local scale = 1
for letter, limit in pairs(suffix_list) do for letter, limit in pairs(suffix_list) do
if math.abs(amount) >= limit then if math.abs(amount) >= limit then
amount = string.format("%.2f", amount / limit) scale = limit
suffix = letter suffix = letter
break break
end end
end end
local k local scaled_value = amount / scale
local formatted = amount local formatted = string.format("%.2f", scaled_value)
while true do -- Split into integer and fractional parts
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", "%1,%2") local integer_part, fractional_part = formatted:match("^(%-?%d+)%.(%d+)$")
integer_part = integer_part or formatted
fractional_part = fractional_part or "00"
if (k == 0) then -- Add commas to integer part
break integer_part = integer_part:reverse():gsub("(%d%d%d)", "%1,"):reverse()
integer_part = integer_part:gsub("^,", ""):gsub("-,", "-")
-- Handle numbers below 1000 without suffix
if scale == 1 then
-- Remove trailing .00 for whole numbers
if fractional_part == "00" then
return integer_part
end end
return string.format("%s.%s", integer_part, fractional_part)
end end
return formatted .. " " .. suffix -- Combine parts and add suffix
return string.format("%s.%s %s",
integer_part,
fractional_part,
suffix
):gsub("%.?0+ %k$", " "..suffix) -- Clean up trailing zeros
end end
--- Display group --- Display group