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! . 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.
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
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.
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.
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
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)