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.