#Trying to show decimals in my value script
1 messages · Page 1 of 1 (latest)
local function formatNumber(num)
local suffixes = {
{1e12, "T"},
{1e9, "B"},
{1e6, "M"},
{1e3, "K"}
}
local absNum = math.abs(num)
local sign = num < 0 and "-" or ""
for _, suffix in ipairs(suffixes) do
if absNum >= suffix[1] then
local value = absNum / suffix[1]
local formatted = (value % 1 == 0)
and string.format("%d", value)
or string.format("%.1f", value)
return sign .. formatted .. suffix[2]
end
end
return tostring(num)
end
print(formatNumber(102000000))
i used chat gpt (probably one of the more useful things that chatgpt can do since maths is NOT my strongsuit
does it work?
yeah BUT if you do
print(formatNumber(102200000)) -- 10.2B
-- you wont get 10.22B
look i hate chat gpt i have never used it but i tried searching for the coregui playerlist module to see how the leaderstats set it (in playerlist youll notice they also do 10.5B or something) - but couldnt find it anywhere... i found that for me i would probably just use chatgpt for this since im dumb with numbers.
thats fair, i have no idea how to make the number as the game has set it in leaderstats
its so bad
look atleast you can easilly add more as its modular
local suffixes = {
-- here you can add {1e15, "Qa"},
{1e12, "T"},
{1e9, "B"},
{1e6, "M"},
{1e3, "K"}
}
im not sure the next number symbol is for these
hey try this might work better
local function formatNumber(num)
local suffixes = {
{1e12, "T"},
{1e9, "B"},
{1e6, "M"},
{1e3, "K"}
}
local absNum = math.abs(num)
local sign = num < 0 and "-" or ""
for _, suffix in ipairs(suffixes) do
if absNum >= suffix[1] then
local value = absNum / suffix[1]
-- Keep up to 4 decimal places, trim trailing zeros
local formatted = string.format("%.4f", value)
formatted = formatted:gsub("0+$", ""):gsub("%.$", "")
return sign .. formatted .. suffix[2]
end
end
return tostring(num)
end
1223500 → 1.2235M
1500000 → 1.5M
2000000 → 2M
BTW this is chatGPT but also suggested this
"If you want, I can also make a simulator-style ultra large number formatter (Qa, Qi, Sx, Sp, Oc, No…) for your Roblox project."
I really want to preference how much i dislike AI to do work for me but this is probably one of the only times i would do something like this... as its a very simple function and isn't going to affect the overall system of my game etc.