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

How to Make a Abbriviated Leaderbaord?

Asked by 4 years ago

Ex: 1,000 4 numbers would be 1k instead or 1,100 would be 1.1k

0
im not very good at scripting can you make the whole thing i dont know how to finish it thx :) Darkrphax -9 — 4y

3 answers

Log in to vote
1
Answered by
Zafirua 1348 Badge of Merit Moderation Voter
4 years ago
Edited 4 years ago

Here is a much easier way

local function cnvrt (n, precission)
    precission = precission or 1;
    if (n >= 10^9) then
        return string.format("%." .. precission .. "fb", n / 10^9)
    elseif (n >= 10^6) then
        return string.format("%." .. precission .. "fm", n / 10^6)
    elseif (n >= 10^3) then
        return string.format("%." .. precission .. "fk", n / 10^3)
    else 
        return tostring(n);
    end
end

print(cnvrt(1548034, 1));
print(cnvrt(17478, 2));

Ad
Log in to vote
0
Answered by
Elixcore 1337 Moderation Voter
4 years ago
Edited 4 years ago

THANK YOU TO GoldAngelInDisguise

For remaking the function, fully working.

here is the code and example of it being ran.

local num = 1239887.45

function ab(num, ignore)
   local fnum = math.floor(num)
   local snum = tostring(fnum)
   local d = 0
   while num >= 1 do
       num = num / 10
       d = d + 1
   end
   num = math.floor(num / 10 ^ (-ignore) + 0.5) * 10 ^ (d - ignore)
   if #snum <= 6 and #snum > 3 then
      local abnum = (num/1000).."K"
      return abnum
   elseif #snum >= 7 then
      local abnum = (num / 1000000).."M"
      return abnum
   end
end

My function:

local num = 1239887.45

function ab(num, ignore)
   local fnum = math.floor(num)
   local snum = tostring(fnum)
   if #snum <= 6 and #snum > 3 then
      local abnum = string.sub(tostring(fnum/1000), 1, ignore + 1).."K"
      return abnum
   elseif #snum >= 7 then
      local abnum = string.sub(tostring(fnum/1000000), 1, ignore + 1).."M"
      return abnum
   end
end

print(ab(num, 0))


made this function in a hurry, basically it simplifies your number and also ignores a certain amount of characters depending on the "ignore" you set.

In the above example, ignore is set to 0 and it will return "1M".

Set it to something high to not ignore anything, and a few if you want to ignore only a few.

Ok I now have time to explain what everything does.

So first we create the function with the number and ignore parameter which will allow the function to well.. function, and be called.

We create fnum, which is a math.floor version of the number to help us see how many characters are in the number using snum, which is transforming the number to a string and then counting the characters.

In the first if statement we check if it's over 999 and under 999,999. (higher than 3 characters, lower or equal to 6 characters).

We then create 'abnum' which is a combination of multiple things, string.sub, concatenation, a division and a transformation from number to string, in order to get everything we need.

by using fnum/division we get what the number would be if it was shortened with "K" or "M". after that we turn it into a string to allow us to combine with with the keyword we want and then use string.sub, what we want to do is use ignore to ignore a certain amount of characters, so it isn't filled with a bunch of decimals, this is something you'll have to work on so it doesn't just return num.M/K

if you set the ignore to a certain number you could get something like "1.M" in the above example it'd be 1.

You need to check for the . and add another character to the ignore so it appears as num.decimal K/M

this was just an example function I made for you, you can perfect it and change it to resolve the problem i told you about above.

0
Ignore what I put. This is beautiful! lol IDKBlox 349 — 4y
Log in to vote
-3
Answered by
IDKBlox 349 Moderation Voter
4 years ago
Edited 4 years ago

What you're going to want is string.sub()

Basically what string.sub(text,start,finish) does is it takes the 'String' or in this case 'NumberValue' or 'IntValue' and changes it into different sections on which we can choose how to deal with it.. Let me explain

local Number = 1500 -- We have one thousand five hundred right here... We want it 1.5K

local newText = string.sub(Number,1,1) .. '.' .. string.sub(Number,2,2) .. 'K'

This is getting the first letter (or number) in 'Number' and placing as newText; using .. '.' .. places that period in between them. doing the string.sub(Number,2,2) gets the second letter in 'Number' and places it after therefor

string.sub(Number,1,1) = 1 -- One

'.' = . -- Period

string.sub(Number,2,2) = 5 -- Five

'K' = K -- The letter K

print(newNumber) -- 1.5K

Every letter or number within Number has a I guess you could call it 'Spot' like in 1500

1 = 1, 5 = 2, 0 = 3, 0 = 4,

as if say 9876543

1 = 9, 2 = 8, 3 = 7, 4 = 6, 5 = 5, 6 = 4, 7 = 3,

if we wanted to get this as 9.8M we'd have to do

local number = 9876543

local shortenedNum = string.sub(number,1,1) .. '.' .. string.sub(number,2,2) ..'M'

print (shortenedNum)
-- 9.8M

This make sense? If not feel free to let me know!

Here I added this for you This will get all numbers under 1 Billion... Feel free to add in 1 billion yourself! Read & learn from this. If you still don't understand. Feel free to ask some more questions!

number = function(value)
    local premade = 'ERROR'
    if value<=999 then
        premade = value
    elseif value >=1000 and value <= 9999 then
        premade = string.sub(value,1,1)..'.'..string.sub(value,2,2)..'K'
    elseif value >=10000 and value<=99999 then
        premade = string.sub(value,1,2)..'.'..string.sub(value,3,3) ..'K'
    elseif value >=100000 and value <=999999 then
        premade = string.sub(value,1,3)..'.'..string.sub(value,4,4) .. 'K'
    elseif value >=1000000 and value <= 9999999 then
        premade = string.sub(value,1,1)..'.'..string.sub(value,2,2)..'M'
    elseif value >=10000000 and value <=99999999 then
        premade = string.sub(value,1,2)..'.'..string.sub(value,3,3)..'M'
    elseif value >=100000000 and value <=999999999 then
        premade = string.sub(value,1,3) .. '.' .. string.sub(value,4,4) .. 'M'
    end
    return premade
end

local a = number(512358) -- This is where you put the numbers
print(a)

Answer this question