Abbreviating Number Values

Hello! I am currently working on a simulator game. I need to abbreviate a number value which will then be applied to a TextLabel. Here is the abbreviation code I made that I need to connect to a number value


local RS = game:GetService("ReplicatedStorage")
local mainFrame = script.Parent 
local rebirthFolder = RS:WaitForChild("RebirthsFolder"):FindFirstChild(mainFrame.Name)
local Abbreviations = {

	"";
	"K";
	"M";
	"B";
	"T";
	"Qd";
	"Qn";
	"Inf";
}

local function AbbreviateNumber(Number)
	for i = 1, #Abbreviations do
		if Number < 10 ^ (i * 3) then
			if Abbreviations[i] == "Inf" then
				return "Inf"
			else
				return math.floor(Number / ((10 ^ ((i-1) * 3)) / 100)) / (100) .. Abbreviations[i]
			end
		elseif tostring(Number) == "Inf" then
			return "Inf"
		end
	end
end

while true do
	wait()
	script.Parent.Text = ""..AbbreviateNumber(rebirthFolder:WaitForChild("Amount").Value)
	script.Parent.Text = ""..AbbreviateNumber(rebirthFolder:WaitForChild("Cost").Value)-- Replace the Strength with your current leaderstats
end

1 Like

Good afternoon, I tried to make a script but I don’t know if it will work.

local RS = game:GetService("ReplicatedStorage")
local mainFrame = script.Parent 
local rebirthFolder = RS:WaitForChild("RebirthsFolder"):FindFirstChild(mainFrame.Name)
local Abbreviations = {

	"";
	"K";
	"M";
	"B";
	"T";
	"Qd";
	"Qn";
	"Inf";
}



local function abbreviateNumber(number)
  local suffixes = {
    "",
    "K",
    "M",
    "B",
    "T",
    "Qd",
    "Qn",
    "Inf"
  }

  local suffixIndex = 1
  while number >= 1000 do
    number = number / 1000
    suffixIndex = suffixIndex + 1
  end

  return string.format("%.1f%s", number, suffixes[suffixIndex])
end


print(abbreviateNumber(12345)) -- 12.3K
print(abbreviateNumber(1234567)) -- 1.2M
print(abbreviateNumber(123456789)) -- 123.5B
print(abbreviateNumber(123456789123)) -- 123456.8T

1 Like

Ill try it, thanks. Ill let you know if it worked

1 Like

One question, will your script abbreviate the TextLabel if placed under the same Parent?

Yes, does this make a bug in the script?

1 Like

Nope, it didnt work I dont know why, It gave me no errors

Also, do you happen to be A builder for hire?

Not planning on using this, but why after ‘Qn’ is it ‘Inf’?

1 Like

Sorry. I’m going to study more to be a good scripter :pensive:

1 Like

It’s because it was in the original script, so I kept that value.

No problem, Thanks for trying. Also, are you a good builder for hire?

Honestly, I think I’m average.

My problem is finding time to build because of my studies and work. So if it’s something for a short period of time, a big project or something like that. I won’t be able to finish the build.

We are currently working on a simulator game and have been looking for a builder. Do you have any experience with simulator maps?

function aberviate(number)
	local abreviations = {
		[3] = "K",
		[6] = "M",
		[9] = "B",
		[12] = "T",
		[15] = "Qa",
		[18] = "Qi",
		[21] = "Sx",
		[24] = "Se",
		[27] = "Oc",
		[30] = "Nn"
	}
	if number < 1000 then
		return number
	elseif string.find(tostring(number),"e+") then
		local split = string.split(tostring(number),"e+")
		if split[1] and split[2] then
			local power = tonumber(split[2])
			if power then
				local abreviation = power - (power % 3)
				local truepre = tonumber(split[1])
				if truepre then
					local preAb = tostring(truepre * (10^(power-(abreviation)))) .. " "
					return preAb .. abreviations[abreviation]
				end
			else
				return number
			end
		end
	elseif not string.find(tostring(number),"e+") then
		local num = tostring(math.floor(number))
		local length = #num
		local trueLength = #num % 3
		if trueLength == 0 then
			trueLength = 3
		end
		local abreviation = abreviations[length-trueLength]
		if abreviation then
			local startNum = string.sub(num, 1, trueLength) .. "."
			local decimals = string.sub(num, trueLength+1,trueLength+2) .. " "
			if startNum and decimals then
				return startNum .. decimals .. abreviation
			else
				return number
			end
		else
			return number
		end
	end
end

By the way, I may as well point out that you don’t really need inf. Any number above (2^1024)-1 which is approximately equal to 1.79*10^308 will automatically count as inf

I don’t have that experience. :sweat_smile: :sweat_smile:

Right now, I have a script to take a number value and plug it into the parts in the TextLabel that I want. I need those number values to be abbreviated when put into the TextLabel. I can send you the script. Maybe I should make it a string

It does, it returns a string of the number and abreviated

I got it to work because of your script. Thanks!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.