Abbreviate Numbers with one line of code!

Hi Guys, so I was thinking about abbrievating number, whether it’s short or long! So I used my 999 Trillion % of my brain and finally found a way of just one line of code! Here it is

local short = {"", "K", "M", "B", "T"} -- Add more

local function abbrievateNum(number)
local str = tostring(math.floor(number))
local substr = string.sub(str, 1, ((#str+2)%3) + 1)
local abbrievation = short[math.floor((#str-1)/3) + 1]
return substr..abbrievation
end
Original code (before asks for readibility & advised to use for faster performance)
local short = {"", "K", "M", "B", "T"} -- Add more

local function abbrievateNum(number)
local str = tostring(math.floor(number))
return string.sub(str, 1, ((#str+2)%3) + 1)..short[math.floor((#str-1)/3) + 1]
end

What qualities the code have ?

  • Faster Performance (use original code)
  • Easy to use
  • Abbreviate very large numbers

If you think, it’s complicated, as I said, I used 999 Trillion % of my brain lol

Anyways, thanks and have a great day!

As this is a site to learn, I can explain the code for developers to understand. Post “Explain” in this topic to get explaination!

I can explain you, but many people may not like my code, I recommend them to make their own topic, because my code would remain the same because it’s one liner. I won’t actually listen much, if you want to rectify my code, you can, but if you want to totally change a code, make your own topic please.

9 Likes

Please could you explain, also in thsi case you HAVE to explain, as it’s a #resources:community-tutorials, not a resource

Isn’t this Community Tutorials ?

Oh, you meant to post in #resources:community-resources? Also I would still like an explanation, if you give it then leave it here

Yes it is

As this is teaching a thing, so I think this is the perfect place.

Okay, but also add an explanation, otherwise it not a #resources:community-tutorials

Alr, cool! So what happens is actually, let’s say you have a 6-digit number, 100000, now we add 2 to it, which is 6+2 = 8. Now, we divide it by 3 (modulus basically, it gives the remainder), now 8%3 gives us 2, so we can see that 2 is extra, like see, 100000 has 6 digits, so it could be evenly divided as 100,000, but now we consider it as 8 digits, so it would be 10,000,000. Here, the first 2 digits 10, is preventing our number to be divided by 3 evenly, now as it has 2 digits extra, we add 1 to it, so it becomes 3, which is 100, now as there is string.sub(), so it goes till the 3rd digit of the number, so it becomes 100. Now the second part of the code just adds a abbreviation. I know I can’t explain well, but I hope you could understand :sweat_smile:

Okay, I was able to understand a little, so basically you do:
5 zeros, add 2, get 10’000’000, add 1 zero, use string.sub, which gets the first 3 numbers, and make the last numbers be a letter, right?

Like, let’s say we have 100,000, add 2, becomes 10,000,000, divide by 3 (modulus), we get 2 (as it’s 8 digits), now add 1, which is 2+1 = 3 and concentate with a letter.

The code you provided doesn’t work: Expected ‘]’ (to close ‘[’ at column 51), got ‘)’

This can also be improved to

local suffixes = {"K", "M", "B", "T"} -- Add more

local function abbreviate(number: number)
	local places = math.floor(math.log10(number) / 3)

	local suffix = suffixes[places]
	if not suffix then return number end

	return (number / 1000^places)..suffix
end

print(abbreviate(69))
print(abbreviate(69_420))
print(abbreviate(1_000_000))
print(abbreviate(1_234_567))

Ok, lemme correct my syntax rq. Also your code is atleast quite big, original code is just one line. Lemme correct it.
EDIT - Corrected it. Try it, it should work!

Pretty sure your code is 5 lines, mine is 7, excluding whitespace. Line count doesn’t really matter though, condensing everything into one line is just less readable.

1 Like

As I said, I already provided explanation, and one line is just easy to use. Also, if you don’t like my code at all, I request you to make another topic with your choice.

The point of a function is to shorten “big code”

What if I reduce it to one line ? Will it be fine now ? And as you talked about readability, I can even explain the code as I stated, so readability isn’t an issue. Also, if you really think my code is bad, I suggest you to make your own topic please.

Ok, so as it seems you’re criticising me a little bit (maybe I’m wrong), so as you stated, the point of a function is to shorten a code, then if you think with a perspective of function, so with this, my line is also one liner. I mean his code is 7 lines, so it becomes one liner with a function, so if I write his code down, it would be one liner. And if I write thousands of code in a function, it would be one liner right ? And with the perspective of function, my code is also one liner and his code too, so there’s no point to criticise me as well because both our code is same with respect to function. And we can even use it without function, I just wrote a function for cleaniness. Also, you can make your own post if you don’t like my code. For criticising, you need a valid point, which point you have to criticise me ? Like any reason, anything bad in my code or what ?

I think what they tried to say was that the code should be made smaller while still being readable.

However, the code in your post looks complicated and intimidating, even for me, and even more so for beginners.

You can shorten a 5000 line script into just 1 line, but that does not make it better.

As I stated, I could even explain it so there isn’t issue for readibility…

Do you think their code is you know very simple ? Just because their lines are spreaded, so it’s kinda easy. But I’m ready to explain, just the problem is no one wants an explaination. And if no one really understand even after I’m ready to explain, that doesn’t mean I can’t post my code. No one is ready to get explained so that isn’t my problem.

It is simply that the readability of your code is severely impacted if you squish it all into one line. It makes it look complicated, and that on it’s own will drive away many people from using it, or even trying to understand it.

More people would be able to understand this code if it wasn’t all on one line, and therefore wouldn’t need to ask you how it works.

Ok, so you want I should seperate this code is 2-3 lines ? And people who have a strive to learn things would ask, and most of the people even copy difficult code, and it’s simple. Just ask me, I have already stated things from my side, so people can’t criticize me on the basis of explaination or whatever. I can explain it. And that doesn’t really make sense that if the code is complex, they won’t use it or just ignore the whole post, tbh I don’t think that makes sense and people who does that are those who don’t wanna learn.