#Trying to show decimals in my value script

1 messages · Page 1 of 1 (latest)

median bear
#

I have this script that makes takes large numbers and puts them into a concatenated form. E.g. 1,000 = 1K, 1,000,000 = 1M. What im struggling with is that I want it to show the decimals too, so instead of 1,500,000 showing 1M, i want it to show 1.5M etc.

stark sandal
#
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

stark sandal
#

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.

median bear
#

its so bad

stark sandal
#

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.

median bear
#

its okay, as long as this works i can give AI a slide

#

and it seems good so far