Abbreviate Numbers with one line of code!

Ok, but I am just saying from what I think (and what others most likely think) about the way you have formatted your code, that just because it has less lines does not make it a better resource, and so saying that in the title is sort of misleading.

I hope you continue making resources, and I was not trying to be hostile.

Ik you weren’t hostile. Also as @2jammers stated, function makes it just one line of code, so title isn’t misleading and I can even fit it in one line.

Hey so, I noticed an issue where your code doesn’t support negative numbers (Due to math.log10)
Here’s a quick commented fix. (So others know what’s going on)

I just use math.abs on the number inputted into math.log10 due to math.log10 not working with numbers less than 0.

local possibleAbbreviations = {"K", "M", "B", "T", "Qd", "Qn"}

local function abbrievate(number: number)
	local places = math.floor(math.log10(math.abs(number)) / 3) -- Get how many 0s are in the number (Nil if not 3 or above. We make the number positive with math.abs due to negative numbers unsupported with the function)
	
	local suffix = possibleAbbreviations[places] -- Get the suffix
	if not suffix then return number end -- Return our original number if there isnt any abbreviations
	
	return (number / 1000^places)..suffix -- Return abbreviated Number
end

The length of your code doesn’t matter get it out of your head! :slightly_smiling_face:. What matters is readability, as said, a function is supposed to shorten code that would take up more lines and make it easier to follow the D.R.Y. principle. The code that @RickAstll3y posted is what it should look like.

1 Like

Ok, so you mean I should make my code long ? I should totally change my code right ? Like the whole code. See, I ain’t too much satisfied with @RickAstll3y code but I can change it, you know make it readable, by dividing into some more lines, for beginners to understand. I can even see most of the people are beginners themselves, so I should probably change my code then. The idealogy would be the same, because I ain’t satisfied with @RickAstll3y code, even @iSophes found a problem, but it’s just readable. So I will surely.make my code divide into lines. And it seemed like he was trying to remove focus from the original code, so that’s why I got mad sorry. I’ll make my code readable thanks :slight_smile:

If you have problem in his code, you can try the original one as well. Because I made this topic for giving my code, but most people just ignore lol

Unfortunately, it doesn’t work. Still has the synatx errors.

Please test your code before posting it, it still doesn’t work. Such a simple function shouldn’t be a whole post anyway.

even @iSophes found a problem, but it’s just readable.

It’s not just more readable, but also more performant. The problem in my code exists in yours too, by the way.

1 Like

He meant, that if I have a code that is only one line, it isn’t always the better, here’s an example:

while true do print(“hi”) wait(1) end

It looks very bad, right?
Instead, it should look like:

while true do
print(“hi”€
wait(1)
end

Mode readable, right? Also, another example of bad code readability is:

local message = “hi”
local function message()
message = “goodbye”
print(message)
end
workspace.Part.Touched:Connect(message)

As you can see, first the function name is vague, which shouldn’t be, as this may be confusing, second, there are no blank lines, which are what makes code actually readable, the code should be:

local message = “hi”
--blank line, because we are going from variables to our function
local function onTouch() --as you can see, the name is less vague
message = “goodbye”
print(message)
end

workspace.Part.Touched:Connect(onTouch)

As you can see it’s more readable, I can’t write everything here, I’ll probably make a #resources:community-tutorials about it

I think you should check Community Resources. A guy made a single line of code, and it is literally good. So that’s totally wrong. And if it’s so simple, why people are saying that it’s complex ?

How though, can you tell me ? You’re claiming that it’s more performant, you should probably give a proof then. Do you know why people are recommending your code ? Just for the sake of readibility, which I’ll add, not for it’s more performant, even my code is not less, it’s just not readable. Also, if you’re so much confident, I recommend you to make your own thread rather than criticizing my code. I won’t change my decision either. Please don’t try to change the original topic. My answer is that you should make your own topic, don’t change original topic.

Is your code working perfectly ? @iSophes found some mistake, so yep, I recommend you to practice yourself what you say. I updated it.

Maybe, the problem is many people are lazy to ask me for explaination and then if they don’t understand, they criticize me. I’ll change my existing code and divide it into lines.

1 Like

I was talking about my code that I will make my code more readable.

I’m not sure if there’s actually a problem with your code, but I will test it and tell you if there’s a problem, I’m not an expert myself so I can tell from here

Ok, please try and do tell me :slight_smile:

1 Like

If you have any problem, just flag this topic. No need to create a matter.

Strings are expensive to create.
local str = tostring(math.floor(number)) this creates a new string
local substr = string.sub(str, 1, ((#str+2)%3) + 1) another new string
str..substr another new string

For proof, it is simple to benchmark.

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[tonumber(((#str-1)/3)) +1]
return str..substr
end

local startTime = os.clock()

for i = 1, 1e5 do
	abbrievateNum(i)
end

print(os.clock() - startTime)

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

local startTime = os.clock()

for i = 1, 1e5 do
	abbreviate(i)
end

print(os.clock() - startTime)

This shows that my function is 2x faster than yours.

I am only trying to show a better way of doing it. I am not going to make a whole topic for that.

I do not need to “practice yourself”. For most use cases, you only need positive numbers.

I don’t know what’s wrong, I changed a few things, the script is:

local number = 100000

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

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

workspace.Baseplate.Touched:Connect(abbreviateNum)

For some strange reason it prints:
100000100

Oh, I actually wrote this code from my mobile, so I really don’t know.