Number Abbreviation

Hello there, I am sure that some of you have problems with number abbreviation. Well, I have created a module where you can abbreviate your numbers.
You can get the module from here or just require(6055441479)

How to use it?
Just change the following base code to your own needs!

local module = require(id or in-game module)

local num = 912048021 --This is just an example number we are going to use

print(module.Simplify(num)) --Should print 912M

--another example

local num = 183497821351421241

print(module.Simplify(num)) --Should print 183.4Qa

--Decimal Update Example

local num = 1946692346719857

print(module.Simplify(num, 5)) --Should print 1.94669Qa 

--Example 2

local num = 1946692346719857

print(module.Simplify(num, 0)) --Should print 1Qa 

--Example 3

local num = 1946692346719857

print(module.Simplify(num)) --Should print 1.9Qa 

--Example 4

local num = 9012

print(module.Simplify(num, 4)) --Should print 9.012K

Source code:

local Back = "000000."

local function NumberNeededWarn(n)
	warn(n.." is not a valid number. Please enter a number")
end

local function FormatNum(No_)
	local a = string.reverse(string.format("%f", No_))
	local c = a:sub(Back:len() + 1)
	return string.reverse(c)
end

local SimplifyerSign = {
	S1 = "K",
	S2 = "M",
	S3 = "B",
	S4 = "T",
	S5 = "Qa",
	S6 = "Qi",
	S7 = "Sx",
	S8 = "Sp",
	S9 = "Oc",
	S10 = "N",
	S11 = "Dc",
	S12 = "Ud",
	S13 = "Dd",
	S14 = "Td",
	S15 = "Qad",
	S16 = "Qid",
	S17 = "Sxd",
	S18 = "Spd",
	S19 = "Ocd",
	S20 = "Nod",
	S21 = "Vg",
	S22 = "Uvg",
	S23 = "Dvg",
	S24 = "Tvg",
	S25 = "Qavg"
}

local module = {}

function module.Simplify(No_, deci)
	local d = FormatNum(tostring(No_))
	local Length = tonumber(#d)
	if deci == nil then
		deci = 1
	else
	end
	if tonumber(d) then
		if Length then
			if Length > 3 then
				local a = math.floor((Length - 1)/3)
				local sign = SimplifyerSign["S"..a]
				local b = math.floor(No_/10 ^ (a * 3 - deci))
				return (b/(10 ^ deci))..sign
			else
				return tostring(No_)
			end
		else
		end
	else
		NumberNeededWarn(d)
		return "NaN"
	end
end

return module

Update log

Edit: Made some changes with @Gojinhan feedback. And also added a feature to calculate the number of decimals you want!

Edit2: Changes with @Blockzez feedback

20 Likes

Simple, effective, and to the point. I’ll be using this, thank you :slight_smile:

3 Likes

Hey cool module, I’m sure it will help a lot of people but i’ve noticed some stuff that don’t need to be there.

For example, you have a needlessly abstracted warn function that’s just a wrapper for the default warn() yet your use context does not warrant such a wrapper. So i’d just use the regular function itself without any extra calls.

You have another pointless abstraction function, “NumberLength”. This can be easily done with much lower amounts of caching and can be optimized a little bit (removing unecessary variables, removing pointless method call, and not tonumbering something that’s already a number) as well as just removing the function, like this:

local length = #tostring(number)

It can be simplified plenty.

Another thing that’s less about actual code performance, but more developer ergonomics and readability. Your naming convention, you’re quite inconsistent with the names you assign to your variables. Some might be camelCase, others might be pascalCase out of nowhere, some might have the full word spelled out, others might be pointlessly abbreviated. It’s better to pick casing and stick with it, arguably the most common variable naming is camelCase for generic variables, pascalCase for functions and table members. Up to you though, you should just be more consistent so it’s easier to read.

Also you don’t need to create and return a module when you’re only have one element, it’d be better to just return the function itself. Also on that topic if you do need to return a table of functions I recommend making use of the luaU table creation optimization, where you define all of the elements in one-go instead of on the fly.

Let me know if you have any questions

Edit: sorry wrong reply lol

3 Likes

Noted, I will make the changes using your suggestions later when I develop.

Edit: Changes have been made

Can you specify the rounding mode and the precision the module has?
Can you be more specific by numbers of decimals? minimum/maximum fraction digits?
Does this support signed numbers, Infinity and NaN?
Does this account for locales?

Is there any reason for why is abbreviating numbers called Simplify in this module?

I have to ask, why does values < 1000 return a number when I expect string (this is completely unacceptable as it should never be a number/double in International or ICU standards)?

print(type(module.Simplify(999))) --> number

Also it errors on a large value:

print(module.Simplify(1E100))

errors attempt to concatenate number with nil.

as well as why there’s a module.Power when we have math.pow and the power of (^) operator.

Is there any particular reason for using tostring on a string (variable d which is a string) then use tonumber on a number (source: # operator on a string)?
Why is inserting an invalid value a throws warning and not an error (or at least treated as NaN)?
Why is there an unnessesary else selection with nothing inside it?

Why should I use this over other implementions of number abbreviations?

2 Likes

Number of decimals in the abbreviated number
Like if you put

print(module.Simplify(56382378, 3))
--Prints 56.382M

What do you mean by signed numbers? NaN means not a number. It does not support infinity and Nan

I created this and used it in games a worked in. I do not want to break any code in those games I worked in.

Thanks for letting me know about that. I am going to change it.

I know there is such an error due to the fact that it does not support such large numbers as I cannot find number abbreviations larger than that as sites give me different answers and nonsense ones as well.

I have changed that

I have changed that. It now returns NaN

It’s your choice. This is just an alternative.

Edit: Do you mind sharing any sites that give you the standard number abbreviations symbols for English if you know any?

The only function that is actually useful is the Simplify function. Other ones are useless.

xRoot: You can just do n^(1/root).
Power: You can just do n^power or use the math.pow function.

I plan to remove them, but I have not re published the module yet

Update: Removed xRoot and Power