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

how to add suffixes to numbers?

Asked by 4 years ago

Hello, fellow beginner here.So i am trying to add cash to my game and i want to abbreviate the numbers using suffixes like K for thousand M for million and so on. And i do know that i didnt attempt it it is because i dont know how to, So please help

0
Just wanted to point out that you should use suffixes such as when the player is not actively interacting with a GUI Object showing it, I suggest that once the player hovers over the GUI Object that the actual number shows, this will point attention to detail :) Chiken983 15 — 4y
0
Great (not) idea Chicken, I'll have that upgrade for 133.337.000.000.000.000.000.000 xD RubenKan 3615 — 4y

2 answers

Log in to vote
2
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
4 years ago
Edited 4 years ago

To put a string after a number, you can use .. To figure out which suffix you need, you can use a loop and a table.

The logic: If a number is divided by 1000 and it is bigger than 1, it means it's still too big for the current suffix, if you loop this you can easily get the correct suffix.

local List = {
    "K","M","B","T","Qd","Qn","Sx","Sp","O","N" --List of your suffixes in order of 1000's
}
function NumberSuffix(Num)

    local ListCount = 0

    -- while we're not at the correct suffix do
    while Num / 1000 >= 1 do

        -- increment the suffix from the array
        ListCount = ListCount + 1 

        -- divide the number so we can continue with it.
        Num = Num / 1000 
    end

    -- if the initial number was < 1000 return the number.
    if ListCount == 0 then return Num end 

    -- returns the number, with the suffix  added at the end
    return math.floor(Num*10)/10 ..List[ListCount].."+"
end


local f = NumberSuffix
print(f(5)) --> 5 
print(f(1337)) --> 1.3K+ 
print(f(30203)) --> 30.2K+ 
print(f(32482348324832)) --> 32.4T+ 
print(f(248738492384293483294823984234)) --> 248.7O+ 

0
This will be helpful, as I (an I'm sure many others) would love to add suffixes to our numbers. NickIsANuke 217 — 4y
0
This process is called concatenation, and it is used in almost all programming languages. Here is a wikipedia article for further reading if anyone is interested: https://en.m.wikipedia.org/wiki/Concatenation sahadeed 87 — 4y
Ad
Log in to vote
-2
Answered by 4 years ago
Edited 4 years ago

--Local Script--

local plr = game.Players.LocalPlayer
local cashNum = tonumber(plr.Cash.Value)
if plr.Cash >=1000
    plr.Cash = tostring(cashNum/1000).."K"
end
if plr.Cash >=1000000 then
    plr.Cash = tostring(cashNum/1000000).."M"
end

If you want to keep going use this

if plr.Cash >= --[[Number you want to convert]] then
    plr.Cash = tostring(cashnum/--[[Number you want to convert]])..--Abbreviation
end

also make sure the cash value type is a string or else it won't work.

0
You'd want to use a loop for this. What you're doing now is overcomplicated, you'd never want to do this if you went into 1e99+, take a look at my answer so you'll know a better way of doing this. RubenKan 3615 — 4y

Answer this question