How to make an Abbreviation System

Make an abbreviation system

by Jonathan64832

Author Avatar

Setup

You'll need to make a script, local script, or module script in order for this to work (works for any script).

Coding

Firstly, you'll need to make a table with the "labels" and the minimum number of number places it has after the first number place. For example:

local places = {
	K = 3; -- 1,000 3 zeroes
	M = 6; -- 1,000,000 6 zeroes
	B = 9; -- 1,000,000,000 9 zeroes
	T = 12; -- 1,000,000,000,000 12 zeroes
} 

Now, you'll want to make a function to input the number and output the abbreviated result.

local places = {
    K = 3; -- 1,000 3 zeroes
    M = 6; -- 1,000,000 6 zeroes
    B = 9; -- 1,000,000,000 9 zeroes
    T = 12; -- 1,000,000,000,000 12 zeroes
} 

local function abbreviate(number) 

end

Check if the number is negative, and enter the amount of decimal places you want to your number.

local places = {
    K = 3; -- 1,000 3 zeroes
    M = 6; -- 1,000,000 6 zeroes
    B = 9; -- 1,000,000,000 9 zeroes
    T = 12; -- 1,000,000,000,000 12 zeroes
} 

local function abbreviate(number) 	
	local negative = number < 0
	local decimalPlaces = 2 -- 1.32k
end

Loop through the labels, and use if statements to see if the number is between two values.

local places = {
    K = 3; -- 1,000 3 zeroes
    M = 6; -- 1,000,000 6 zeroes
    B = 9; -- 1,000,000,000 9 zeroes
    T = 12; -- 1,000,000,000,000 12 zeroes
} 

local function abbreviate(number)     
    local negative = number < 0
    local decimalPlaces = 2 -- 1.32k

	for label, value in pairs(places) do
		if number > 10^value and number < 10^(value+3) then -- checks if the number is higher than the current value but lower than the next value

		end
	end
end

Make another function to add the decimal places to the number.

local places = {
    K = 3; -- 1,000 3 zeroes
    M = 6; -- 1,000,000 6 zeroes
    B = 9; -- 1,000,000,000 9 zeroes
    T = 12; -- 1,000,000,000,000 12 zeroes
} 

local function addDecimalPlaces(number, decPl)
	local multiplier = 10^decPl
	return math.floor(number*multiplier)/multiplier
end

local function abbreviate(number)     
    local negative = number < 0
    local numberOfDecimalPlaces = 2 -- 1.32k

    for label, value in pairs(places) do
        if number > 10^value and number < 10^(value+3) then -- checks if the number is higher than the current value but lower than the next value

        end
    end
end

Create the abbreviated form using a mixture of the label and the decimal places.

local places = {
    K = 3; -- 1,000 3 zeroes
    M = 6; -- 1,000,000 6 zeroes
    B = 9; -- 1,000,000,000 9 zeroes
    T = 12; -- 1,000,000,000,000 12 zeroes
} 

local function addDecimalPlaces(number, decPl)
    local multiplier = 10^decPl
    return math.floor(number*multiplier)/multiplier
end

local function abbreviate(number)     
    local negative = number < 0
    local numberOfDecimalPlaces = 2 -- 1.32k

	for label, value in pairs(places) do
		if number > 10^value and number < 10^(value+3) then  -- checks if the number is higher than the current value but lower than the next value
			local abbreviatedForm = addDecimalPlaces(number/(10^value), numberOfDecimalPlaces) .. label -- number/(10^value) returns the number abbreviated : 9203432 = 9.2
		end
	end
end

Check if the number is negative. If it is, add the negative sign before it; if it isn't, return the abbreviated form.

local places = {
    K = 3; -- 1,000 3 zeroes
    M = 6; -- 1,000,000 6 zeroes
    B = 9; -- 1,000,000,000 9 zeroes
    T = 12; -- 1,000,000,000,000 12 zeroes
} 

local function addDecimalPlaces(number, decPl)
    local multiplier = 10^decPl
    return math.floor(number*multiplier)/multiplier
end

local function abbreviate(number)     
    local negative = number < 0
    local numberOfDecimalPlaces = 2 -- 1.32k

	for label, value in pairs(places) do
		if number > 10^value and number < 10^(value+3) then  -- checks if the number is higher than the current value but lower than the next value
			local abbreviatedForm = addDecimalPlaces(number/(10^value), numberOfDecimalPlaces) .. label -- number/(10^value) returns the number abbreviated : 9203432 = 9.2

			if negative then
				return "-" .. abbreviatedForm
			else
				return abbreviatedForm
			end
		end
	end
end

Lastly, if the function takes in a number that doesn't need to be abbreviated, just return the number as its regular self or its negative self.

local places = {
    K = 3; -- 1,000 3 zeroes
    M = 6; -- 1,000,000 6 zeroes
    B = 9; -- 1,000,000,000 9 zeroes
    T = 12; -- 1,000,000,000,000 12 zeroes
} 

local function addDecimalPlaces(number, decPl)
    local multiplier = 10^decPl
    return math.floor(number*multiplier)/multiplier
end

local function abbreviate(number)     
    local negative = number < 0
    local numberOfDecimalPlaces = 2 -- 1.32k

	for label, value in pairs(places) do
		if number > 10^value and number < 10^(value+3) then  -- checks if the number is higher than the current value but lower than the next value
			local abbreviatedForm = addDecimalPlaces(number/(10^value), numberOfDecimalPlaces) .. label -- number/(10^value) returns the number abbreviated : 9203432 = 9.2

			if negative then
				return "-" .. abbreviatedForm
			else
				return abbreviatedForm
			end
		end
	end
	if negative then return number * -1 end
	return number
end

Test it.

local places = {
    K = 3; -- 1,000 3 zeroes
    M = 6; -- 1,000,000 6 zeroes
    B = 9; -- 1,000,000,000 9 zeroes
    T = 12; -- 1,000,000,000,000 12 zeroes
} 

local function addDecimalPlaces(number, decPl)
    local multiplier = 10^decPl
    return math.floor(number*multiplier)/multiplier
end

local function abbreviate(number)     
    local negative = number < 0
    local numberOfDecimalPlaces = 2 -- 1.32k

	for label, value in pairs(places) do
		if number > 10^value and number < 10^(value+3) then  -- checks if the number is higher than the current value but lower than the next value
			local abbreviatedForm = addDecimalPlaces(number/(10^value), numberOfDecimalPlaces) .. label -- number/(10^value) returns the number abbreviated : 9203432 = 9.2

			if negative then
				return "-" .. abbreviatedForm
			else
				return abbreviatedForm
			end
		end
	end
	if negative then return number * -1 end
	return number
end

print(abbreviate(3264317323)) 

##Output

3.26B

##Why this is important

If you are making a simulator game, it's ideal to abbreviate your stats in order to make the text simpler to read and understand. For example:

In a local script inside a TextLabel:

local player = game.Players.LocalPlayer
local TextLabel = script.Parent

local places = {
    K = 3; -- 1,000 3 zeroes
    M = 6; -- 1,000,000 6 zeroes
    B = 9; -- 1,000,000,000 9 zeroes
    T = 12; -- 1,000,000,000,000 12 zeroes
} 

local function addDecimalPlaces(number, decPl)
    local multiplier = 10^decPl
    return math.floor(number*multiplier)/multiplier
end

local function abbreviate(number)     
    local negative = number < 0
    local numberOfDecimalPlaces = 2 -- 1.32k

    for label, value in pairs(places) do
        if number > 10^value and number < 10^(value+3) then  -- checks if the number is higher than the current value but lower than the next value
            local abbreviatedForm = addDecimalPlaces(number/(10^value), numberOfDecimalPlaces) .. label -- number/(10^value) returns the number abbreviated : 9203432 = 9.2

            if negative then
                return "-" .. abbreviatedForm
            else
                return abbreviatedForm
            end
        end
    end
    if negative then return number * -1 end
    return number
end

while wait() do
	TextLabel.Text = abbreviate(player.leaderstats.Coins.Value) .. " Coins"
end

View in-game to comment, award, and more!