Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How do I abbreviate a large number in a GUI?

Asked by 6 years ago

Ok so I'm making a clicker game and you can get bux by clicking or buying upgrades, so when this gui value hits 1 million I want it to to say 1M instead of just 1000000

But I haven't figured out how to do it since most ways are only for the leaderboard

How do I turn this into that:

local plr = game.Players.LocalPlayer
while wait() do
    script.Parent.Text = "Bux: "..plr.leaderstats.Bux.Value
end

sry if I don't make any sense at all

4 answers

Log in to vote
1
Answered by
DanzLua 2879 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago
function abb(n)
    if n>=1*10^4 and n<1*10^6 then
        n=(n/1*10^-3)
        return string.sub(tostring(n),1,5).."K"
    elseif n>=1*10^6 and n<1*10^7 then
        n=(n/1*10^-6)
        return string.sub(tostring(n),1,3).."M"
    elseif n>=1*10^7 and n<1*10^8 then
        n=(n/1*10^-7)
        return string.sub(tostring(n),1,3).."B"
    elseif n>=1*10^8 and n<1*10^9 then
        n=(n/1*10^-8)
        return string.sub(tostring(n),1,3).."T"
    else
        return n
    end
end

print(abb(134444564))
--> 1.3T

This function take the parameter n and checks to see if it is between certain number ranges.

For example, the M statement is

if n>=1*10^4 and n<1*10^5 then

110^6 is scientific notation for 1000000 While 110^7 is 10000000

If n is between those two number I then take the number and turn it into a scientific number but without the exponent part at the end. EX:

local n = 1233344
n=(n/1*10^-6) --> 1.233344

with n turned into this we can then go on to return the rounded number (not really round in this cause since im just taking the actual string characters) and then add the appropriate letter at the end

local n = 1.233344
return string.sub(tostring(n),1,3).."M" --> 1.2M

string.sub is used to get the first 3 letters of the now string (tostring()). "1" "." "2" are the three "letters". This wont break if the number is like exactly 1 too.

For the thousands check I allowed there to be up to 5 letters since we could have like 444.4K as a number.

Please ask questions if you have any.

Edited to fix 1k to 999k range.

0
Where Do I Put It? rjthecoolkid1215 4 — 4y
Ad
Log in to vote
1
Answered by
xPolarium 1388 Moderation Voter
6 years ago
Edited 6 years ago

Prefer using string patterns and a table instead with the abreviations inside it.

Looking at the table in this wiki page you can notice a pattern in how you can get large numbers just by raising 6 to 10 (Thats for a million).

local LargeNumberNames = {"M","B","T"}

function SmallerTag(number)
    local tag;

    for i = #LargeNumberNames, 1, -1 do
        --Using built-in function to calculate which large number it would fall in
        local a = math.pow(10, (i+1)*3)
        if number >= a then
            --Format our tag into a string.
            --We then can get our abbreviation using the index, i.
            tag = string.format("%.2f", number/a)..LargeNumberNames[i]
            return tag  
        end
    end
end


print(SmallerTag(1234567890)) --prints 1.2B

Line 9, I had used this to convert the float to a string and just getting the first 2 decimals.

edit: fixed number > a to number >= a.

prefix wasn't the right word for these tags.

Log in to vote
0
Answered by
RayCurse 1518 Moderation Voter
6 years ago
Edited 6 years ago

You can do something like this:

function abbreviateNumber (num)
    if num <= 9999 then
        return v
    elseif num >= 1e12 then
        --trillion
        return string.format("%.1f trillion", num/1e12)
    elseif num >= 1e9 then
        --billion
        return string.format("%.1f billion", num/1e9)
    elseif num >= 1e6 then
        --million
        return string.format("%.1f million", num/1e6)
    elseif num >= 1e3 then
        --thousand
        return string.format("%.1fk", num/1e3)
    end
end

The function uses if statements to check the range of the value given, formats the value using string.format() and returns the result.

The numbers are represented in scientific notation for brevity's sake. For example, 1e12 means 1 followed by 12 zeroes. I'm using string.format() here so that it displays the number to one decimal digit. It formats numbers up to the trillions but you can increase the range if it is needed.

Log in to vote
0
Answered by 6 years ago

Thank you alot! Both of you

0
Use comments instead RayCurse 1518 — 6y

Answer this question