Hello fellow devforumers!
As I’m sure most of you are already familiar with Number Abbreviation and what it is, I’m not going to explain it in detail and I’m just going to get straight to the point.
I’ve seen tens, if not hundreds of Number Abbreviators. But I’ve always considered them quite hard to understand for most “noobs”. So I decided to shorten it down to just one line.
Here’s the Main Line:
local abbreviatedNumber = math.floor((((Number/1000^(math.floor(math.log(Number, 1e3))))*100)+0.5)) /100 .. (suffixes[math.floor(math.log(Number, 1e3))] or "")
Now, this seems way too complicated, so let’s break it down into a couple pieces.
First, let’s make a function.
function AbbreviateNumber(Number)
end
Now, let’s create a simple value, let’s name it Bob.
KEEP IN MIND I DON’T KNOW ANY MATH SO I’LL JUST USE THE DEVHUB EXPLANATIONS
local Bob = math.floor(math.log(Number, 1e3))
-- We get the logarithm of the number and use math.floor to "get
--the largest integer smaller than or equal to the number"
Now we make a dividable value that will help us later on.
local DivValue = 1000^Bob
Then, you make a function that will round the number.
function roundToHundred(number)
return math.floor(((number*100)+0.5))/100
end
Finally, we do this and get the final number.
local finalNumber = roundhundreth(Number/ DivValue) .. (suffixes[Bob] or "")
Of course, you also need to add suffixes before anything, so at the top of your script, add this
local suffixes = {"K", "M", "B", "T", "Q"}
- Now, you could combine all of them and make a simple module that should work just fine, but as I said, I want it to be REALLY short, so let’s just combine them all and forget about variables!
math.floor((((number/1000^(math.floor(math.log(number, 1e3))))*100)+0.5)) /100 .. (suffixes[math.floor(math.log(number, 1e3))] or "")
Of course I know most people just scrolled to the end, so here’s a final working version:
local suffixes = {"K", "M", "B", "T", "Q"}
function roundNumber(number)
local finalNr = math.floor((((number/1000^(math.floor(math.log(number, 1e3))))*100)+0.5)) /100 .. (suffixes[math.floor(math.log(number, 1e3))] or "")
return finalNr
end
To make it work just call the function
print(roundNumber(1000)) -- gonna print 1K
Thank you for reading through this, have a wonderful day!
If you have any feedback feel free to let me know!
I would like to give a huge thank you to TimesIllusion for this info and for his module (which is really helpful)