Simulator Number Abbreviation System With Custom Decimals

So I created a number abbreviation system, because I couldn’t seem to find any good ones on youtube or the devforum.

Module With Unnecessary Variables:

local Module = {}

local Abbreviations = {"k", "M", "B", "T", "Qa", "Qn", "Sx", "Sp", "Oc", "N"} -- Number Abbreviations

local f = math.floor --- Rounds down for example 1.99 becomes 1
local l10 = math.log10 -- Checks how many digits are in a number

function Module.AbbreviateNumber(Number: number, Decimals)
	return f(((Number < 1 and Number) or f(Number) / 10 ^ (l10(Number) - l10(Number) % 3)) * 10 ^ (Decimals or 3)) / 10 ^ (Decimals or 3)..(Abbreviations[f(l10(Number) / 3)] or "")
end

print(Module.AbbreviateNumber(1.2751232142143e8)) -- 127.512M
print(Module.AbbreviateNumber(1.2751232142143e7)) -- 12.751M
print(Module.AbbreviateNumber(1.2751232142143e6)) -- 1.275M

print(Module.AbbreviateNumber(1.2751232142143e8, 2)) -- 127.51M
print(Module.AbbreviateNumber(1.2751232142143e7, 2)) -- 12.75M
print(Module.AbbreviateNumber(1.2751232142143e6, 2)) -- 1.27M

print(Module.AbbreviateNumber(1.2751232142143e8, 1)) -- 127.5M
print(Module.AbbreviateNumber(1.2751232142143e7, 1)) -- 12.7M
print(Module.AbbreviateNumber(1.2751232142143e6, 1)) -- 1.2M

print(Module.AbbreviateNumber(1.2751232142143e8, 0)) -- 127M
print(Module.AbbreviateNumber(1.2751232142143e7, 0)) -- 12M
print(Module.AbbreviateNumber(1.2751232142143e6, 0)) -- 1M

return Module

Module Without Unnecessary Variables

local Module = {}

function Module.AbbreviateNumber(Number: number, Decimals)
	return math.floor(((Number < 1 and Number) or math.floor(Number) / 10 ^ (math.log10(Number) - math.log10(Number) % 3)) * 10 ^ (Decimals or 3)) / 10 ^ (Decimals or 3)..(({"k", "M", "B", "T", "Qa", "Qn", "Sx", "Sp", "Oc", "N"})[math.floor(math.log10(Number) / 3)] or "")
end

print(Module.AbbreviateNumber(1.2751232142143e8)) -- 127.512M
print(Module.AbbreviateNumber(1.2751232142143e7)) -- 12.751M
print(Module.AbbreviateNumber(1.2751232142143e6)) -- 1.275M

print(Module.AbbreviateNumber(1.2751232142143e8, 2)) -- 127.51M
print(Module.AbbreviateNumber(1.2751232142143e7, 2)) -- 12.75M
print(Module.AbbreviateNumber(1.2751232142143e6, 2)) -- 1.27M

print(Module.AbbreviateNumber(1.2751232142143e8, 1)) -- 127.5M
print(Module.AbbreviateNumber(1.2751232142143e7, 1)) -- 12.7M
print(Module.AbbreviateNumber(1.2751232142143e6, 1)) -- 1.2M

print(Module.AbbreviateNumber(1.2751232142143e8, 0)) -- 127M
print(Module.AbbreviateNumber(1.2751232142143e7, 0)) -- 12M
print(Module.AbbreviateNumber(1.2751232142143e6, 0)) -- 1M

return Module
16 Likes

Did a quick search using “decimal places” and found this too:

1 Like

That looks like a waste of code to me and unnecessary looping through tables.

2 Likes

Why the unnecessary module? Just pack everything that’s needed into the function.

2 Likes

So it can be used outside of the module script. It’s supposed to be for module scripts but you can pack it into a function if you’re only going to use it for 1 script

i just dont like these unnessecary variables

2 Likes

It’s to shorten the script, because I’m using both of them a lot inside of the code. If I don’t the code will become super long and I also said that you could put the whole code in one line.

this dosent really “shorten” the script. if youre really wanting to “shorten” the script then just try to simplify the code instead, its not good practice to assign useless variables if its already there

I meant that it shortens the 1 line of code inside of the function making it easier to read and change

If you want it readable, don’t do it all in one line, they don’t like to read horizontally

also, shorter code DOES NOT mean better code

1 Like

It’s a community resource not a tutorial also whether or not I use variables or not doesn’t change how the code works

yes, a resource that needs to be readable, you even wanted it to be “readable”

even if its not a tutorial, its an resource, it still needs to be readable

Me using many math.floor and math.log10 does not make it a better code. Also it is very easy to read. It’s a short sentence

1 Like

… wait so your entire code is bad? did you realize you used math.floor and math.log10 just in variables?

if you want to continue, dm me instead

really? its basically reading a long story saying its a short sentence

wow, how short!

i can read this!

if (X - X + X * 2 % 5 * 7) and (2 * 7 ^ 3 % 4 * 5 * 2 % 4) and (2 * 7 % 3 * 2 % 7 * 2 ^ 6) then
return true
end
1 Like

If you have any suggestions on how I could shorten it feel free to say so

what’s the use of this?There are already millions of things like this out there. What’s the difference with this and the other ones?

well, actually you could simplify this ALOT, take a look at this, its not mine but its actually fast and too simplified, other than this method

now this is more readable

I suggest you try his code which doesn’t even work for high numbers and doesn’t let you customly choose the amount of decimals

If you want to change the numbers of decimals you can just do %0.01. Just because you added 1 little feature to yours doesn’t make it better than the others.

Mynumber = abbreviation from other module
Mynumber = Mynumber - Mynumber % 0.01

Add more 0s after the decimals to see more/ less of those decimals

try to recreate this code but make it more “customizable” as you said, it would be readable