Integer Formatter

Does anyone have the integer formatter to change numbers into like “…K” and “…M”, etc… This would help a lot!

2 Likes

I suppose you could use something like this:

local ValueTables = {
[3] = "K"
[6] = "M"
[9] = "B"
}

function Format(n)
local Suffix = nil
n = tostring(n) 
local IntTable = {}
for i, v in pairs(n:split("") do
table.insert(IntTable, v)
end

for i, v in pairs(IntTable) do
local iterator = 0
if v == "0" then
iterator += 1
end
if TableOfValues[iterator] then
Suffix = TableOfValues[iterator]
end
end

local EndVar = ""
for i, v in pairs(IntTable) do
EndVar..v
end

if Suffix ~= nil then
return EndVar..Suffix
end

return "Couldn't format!"
end

I haven’t tested it though, so there’s probably errors.

local Formats = {
    'fard', 'fardfard'
}

function FindIndexOfValue(List, Value)
	for _, value in next, List do
		if Value == value then
			return _
		end
	end
end

function form(n)
	local result
	for _, value in pairs(Formats) do
		local index = FindIndexOfValue(Formats, value)
		local hmm = 3 * index
		if n >= 10 ^ hmm then
			result = ('%s%s'):format(n/(10^hmm), value)
		end
	end
	return result
end

I use:

local module = {}

local SUFFICES =
	{
		'K', 'M', 'B', 'T', 'Qa', 'Qi', 'Sx', 'Sp', 'Oc', 'No', 'Dc', 'Ud',
		'Dd', 'Td', 'Qad', 'Qid', 'Sxd', 'Spd', 'Od', 'Nd', 'V', 'Uv', 'Dv',
		'Tv', 'Qav', 'Qiv', 'Sxv', 'Spv', 'Ov', 'Nv', 'Tt',
	}

function module.Suffix(Number)
	if Number < 1000 then return Number end

	local Dec = math.floor(math.log10(Number))
	Dec = math.floor(Dec / 3)
	
	local DivNumber = (Number / 1000 ^ Dec)
	return tostring(math.floor(DivNumber * 100) / 100) .. SUFFICES[Dec]
end

return module

Looks good except for the inconsistent use of pairs and next. Pairs is an iterater function and should be used in favor of next. However, pairs uses next. You should always use pairs.

it is the same, i found no difference between next and pairs().

That is because pairs is a convenience function. However, you shouldn’t be alternating between the two. Also ROBLOX reccomend you use pairs. Basically pairs uses next and they function the same way, but pairs should be used for code readability plus more.

sorry i dont know what this means in indonesia?

Basically it means it makes coding easier and is overall made for ease of use.

i know this is already solved but @ABHI1290 created a community resource that accomplishes the same goal far more succinctly

local suffixes = {"", "K", "M", "B", "T", "QA", "QI", "SX", "SP", "OC", "NO", "DC", "UD", "DD", "TD", "QAD", "QID", "SXD", "SPD", "OCD", "NOD", "VG", "UVG"}

local function abbreviateNumber(n)
	-- CoolAbhi's Number Abbreviation System V 2.3
	local s = tostring(math.floor(n))
	return string.sub(s, 1, ((#s - 1) % 3) + 1) .. suffixes[math.floor((#s - 1) / 3) + 1]
end