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:

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

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
01function abb(n)
02    if n>=1*10^4 and n<1*10^6 then
03        n=(n/1*10^-3)
04        return string.sub(tostring(n),1,5).."K"
05    elseif n>=1*10^6 and n<1*10^7 then
06        n=(n/1*10^-6)
07        return string.sub(tostring(n),1,3).."M"
08    elseif n>=1*10^7 and n<1*10^8 then
09        n=(n/1*10^-7)
10        return string.sub(tostring(n),1,3).."B"
11    elseif n>=1*10^8 and n<1*10^9 then
12        n=(n/1*10^-8)
13        return string.sub(tostring(n),1,3).."T"
14    else
15        return n
16    end
17end
18 
19print(abb(134444564))
20--> 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

1if 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:

1local n = 1233344
2n=(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

1local n = 1.233344
2return 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).

01local LargeNumberNames = {"M","B","T"}
02 
03function SmallerTag(number)
04    local tag;
05 
06    for i = #LargeNumberNames, 1, -1 do
07        --Using built-in function to calculate which large number it would fall in
08        local a = math.pow(10, (i+1)*3)
09        if number >= a then
10            --Format our tag into a string.
11            --We then can get our abbreviation using the index, i.
12            tag = string.format("%.2f", number/a)..LargeNumberNames[i]
13            return tag 
14        end
15    end
16end
17 
18 
19print(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:

01function abbreviateNumber (num)
02    if num <= 9999 then
03        return v
04    elseif num >= 1e12 then
05        --trillion
06        return string.format("%.1f trillion", num/1e12)
07    elseif num >= 1e9 then
08        --billion
09        return string.format("%.1f billion", num/1e9)
10    elseif num >= 1e6 then
11        --million
12        return string.format("%.1f million", num/1e6)
13    elseif num >= 1e3 then
14        --thousand
15        return string.format("%.1fk", num/1e3)
16    end
17end

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