The "shortest" way to "shorten" a number

Hello fellow devforumers!:wave:

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)

Time’s NumberCounter Module

4 Likes

I’m not a “noob” but I can barely understand what you did here or why. Flattening everything into a single line and ditching both readability and maintainability does not make this any easier to comprehend. It’s precisely because those other abbreviators are expanded out that you can actually see the flow of logic and try and see what’s going on.

Flattening all this logic into one line =/= a better resource. It just makes it more complicated to read and harder to unroll if you want to make changes to the logic in the future if necessary.

3 Likes

Fair point. What I did try to achieve was a short number Abbreviation system. And I did exactly that. I explained it the best I could.

Also, small chances any additional changes would be needed because it already works well enough as it is.

This might be a bit more readable, probably works the exact same but readability is always nice.

local suffixes = {'','K','M','B','T','Q','QN','S','SP','O','N','D','UD','DD'}
local stringed
for i = 1, #suffixes do
	if number < 10^(i*3) then
		stringed = math.floor(number/((10^((i-1)*3))/100))/(100)..suffixes[i]
		break
	end
end
return stringed or number
2 Likes

As a person who likes to code golf, this is nice. I’m pretty bad at this type of stuff, and seeing it shortened in so little bytes interests me as a code golfer. Now imagine writing this in a golfing language. (I totally didn’t make a golfing version of Lua one time.)

1 Like