Abbreviation script not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

i want to abbbreviate every number and when it reaches 1,000T it turns into 1aa and 1ab and 1ac like idle miner

  1. What is the issue? Include screenshots / videos if possible!

when the abbreviation reached 1ac or higher it just turns into 1.eK

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

the script might be wrong

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

btw my mouse is broken so i cant select all the words…

script:1

local currencyfolder = game.ReplicatedStorage.GameData.Currencies
local Abbrev = require(game.ReplicatedStorage.Abbreviate)

while wait() do
	script.Parent.Coin.Text = Abbrev.abbreviate(currencyfolder.Coins.Value)
	script.Parent.DawnCoin.Text = Abbrev.abbreviate(currencyfolder.DawnCoins.Value)
	script.Parent.DuskCoin.Text = Abbrev.abbreviate(currencyfolder.DuskCoins.Value)
	script.Parent.FireCoin.Text = Abbrev.abbreviate(currencyfolder.FireCoin.Value)
	script.Parent.IceCoin.Text = Abbrev.abbreviate(currencyfolder.IceCoins.Value)
	script.Parent.SuperCash.Text = Abbrev.abbreviate(currencyfolder.SuperCash.Value)
end

my abbreviation module:
Note: the script wasent made by me

local module = {}


function module.abbreviate(number) -- number is the number that we will send when we are calling the function
	local abbreviations = { -- now we need a dictionary for our abbreviations so we put the abbreviation first then the ammount of digits for example ['K'] = 4,
		['K'] = 4,
		['M'] = 7,
		['B'] = 10,
		['T'] = 13,
		['aa'] = 16,
		['ab'] = 19,
		['ac'] = 22,
		['ad'] = 24,
		['ae'] = 27,
		['af'] = 30,
		['ag'] = 33,
		['ah'] = 36,
		['ai'] = 39,
		['aj'] = 42,
		['ak'] = 45,
		['al'] = 48,
		['am'] = 51,
		['an'] = 54,
		['ao'] = 57,
		['ap'] = 60,
		['aq'] = 63,
		['ar'] = 66,
		['as'] = 69,
		['at'] = 72,
		['au'] = 75,
		['av'] = 78,
		['aw'] = 81,
		['ax'] = 84,
		['ay'] = 87,
		['az'] = 90,
		['ba'] = 93,
		['bb'] = 96,
		['bc'] = 99,
		
	}

	local abbreviatedNumber = {} -- an empty table that we will fill after

	local text = tostring(math.floor(number)) -- converting the number to a string (math.floor rounds the number)
	if #text > 3 then -- checking if the number is big enough to be abbreviated
		for i, abbreviation in pairs(abbreviations) do -- finding the abbreviation
			if #text >= abbreviation and #text < (abbreviation + 3) then 
				for abbreviation in text:gmatch(".") do -- looping through the string
					table.insert(abbreviatedNumber, abbreviation)  -- inserting each digit of the number to the empty table
				end
				if #text == abbreviation then -- checking if the number of digits is equal to the abbreviation 
					return abbreviatedNumber[1]..'.'..abbreviatedNumber[2]..i -- we get the first digit and the second digit and we seperae them with a '.' then we add abbreviation for example 1.5K
				elseif #text == (abbreviation + 1) then -- we check if the number has 1 more digit than the abbreviation like the number 15000 
					return abbreviatedNumber[1]..abbreviatedNumber[2]..i -- we connect the first and second digit like 15K (without the '.')
				elseif #text == (abbreviation + 2) then --  we check if the number has 2 more digit than the abbreviation like the number 150000
					return abbreviatedNumber[1]..abbreviatedNumber[2]..abbreviatedNumber[3]..i -- we get the first 3 digits of the number then we add the abbreviation
				end
			end
		end
	else
		return text -- if we can't abbreviate the number we return it how it is.
	end

end


return module 

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

i mean that if the amount of digit reaches 22 or higher it turns into 1.eK

You could just take the code from this

And customize it

Theres also a way of telling how many digits are in a number (if it helps):

local Digits = math.ceil(math.log(Number))

-- Better ex
local Digits = math.ceil(math.log10(20515482))
print(Digits) -- Prints 8

Which could help with counting how many digits there are and customizing the other script
(if … digits add …)

error i got: attempt to index number with number

i tried another model now it saying 1000ab

new script:

local AbbreviateNumber = {}

local Abbreviations = {
	K = 4,
	M = 7,
	B = 10,
	T = 13,
	aa = 16,
	ab = 19,
	ac = 22,
	ad = 25,
	ae= 28,
	af = 31,
	ag = 34,
	ah = 37,
	ai = 40,
	aj = 43,
	ak = 46,
	al = 49,
	am = 52,
	an = 55,
	ao = 58,
	ap = 61,
	aq = 64,
	ar = 67,
	as = 70,
	at = 73,
	au = 76,
	av = 79,
	aw = 82,
	ax = 85,
	ay = 88,
	az = 91,
	ba = 94,
	bb = 97,
	bc = 100,
	bd = 103,
	be = 106,
	bf = 109,
	bg = 112,
	bh = 115,
	bi = 118,
	bj = 121,
	bk = 124,
	bl = 127,
	bm = 130,
	bn = 133,
	bo = 136,
	bp = 139,
	bq = 142,
	br = 145,
	bs = 148,
	bt = 151,
	bu = 154,
	bv = 157,
	bw = 160,
	bx = 163,
	by = 166,
	bz = 169,
	ca = 172,
	cb = 175,
	cc = 178,
	cd = 181,
	ce = 184
}

function AbbreviateNumber.abbreviate(number)
	local text = tostring(math.floor(number))
	
	local chosenAbbreviation
	for Abbreviation, digits in pairs(Abbreviations) do
		if #text >= digits and #text < (digits + 3) then
			chosenAbbreviation = Abbreviation
			break
		end
	end
	
	if chosenAbbreviation then
		local digits = Abbreviations[chosenAbbreviation]
		
		local rounded = math.floor(number / 10 ^ (digits - 2)) * 10 ^ (digits - 2)
		
		text = ""..string.format("%.1f", rounded / 10 ^ (digits - 1))..chosenAbbreviation
	else
		text = ""..number
	end
	
	return text
end

return AbbreviateNumber

and it also sometimes say 100000000000aa or 100000000ab or 100000000000000000000K

why is it happining

oh yea the game im making is idle miner but roblox

this code must work fine. by @ABHI1290

function abbreviateNumber (n)
	-- Abhi's Number Abbreviation System V 2.3
	local s = tostring(math.floor(n))
	return string.sub(s, 1, ((#s - 1) % 3) + 1) .. ({"", "K", "M", "B", "T", "QA", "QI", "SX", "SP", "OC", "NO", "DC", "UD", "DD", "TD", "QAD", "QID", "SXD", "SPD", "OCD", "NOD", "VG", "UVG"})[math.floor((#s - 1) / 3) + 1]
end

its stuck at 1QI and 1SX and 1.SX

whhhhhhhhhhhaaaaaaaaaaaYYYYYYYYYYYYYYYYYYYYY

atleast 1SX is ac

BUT I DONT WANT TO BE STUCK WITH 1 QI (ab) AND 1SX (ac)

when i try to enter number bigger than 1SX it just display as 1QI and 1SX and 1.SX

you can do something like this

local suffixList = {"k", "M", "B" , "T", "Qa", "Qi", "s", "Sp", "O", "N", "D", "Und", "Dnd", "Trd", "Qad", "Qid", "Sid", "Spd", "Ocd", "Ncd", "V", "G", "Gp"}

local function NumberToString(value)
    local index = math.max(math.floor(math.log10(math.abs(value)) / 3), 0)
    value /= math.pow(10, index * 3)
    local suffix = suffixList[index] or ""
    return string.format("%.2f%s", value, suffix)
end

local number = math.random(-1000000, 1000000)
local text = NumberToString(number)
print(text, number)

but if you want to work with very large numbers and not get any errors then you can do something like this

local abbreviations = {"", "K", "M", "B", "T", "Q"}
local numbers = {0, 0, 0, 0, 0, 0}

-- as long as you don't add very large numbers this should be accurate
local function Add(value)
    numbers[1] += value
    for i = 2, #numbers do
        local floor = math.floor(numbers[i-1] / 1000)
        numbers[i-1] -= floor * 1000
        numbers[i] += floor
    end
end

-- this might not be accurate as we are compressing all variables into one
local function GetValue()
    local value = 0
    for i = 1, #numbers do
        value += numbers[i] * 1000 ^ (i - 1)
    end
    return value
end

-- this might not be accurate as we are compressing all variables into one
local function GetAbbreviations()
    for i = #numbers, 1, -1 do
        if numbers[i] == 0 then continue end
        local value = 0
        local count = 0
        for j = i, 1, -1 do
            value += numbers[j] / (1000 ^ count)
            count += 1
        end
        return value .. abbreviations[i]
    end
end

-- this will be accurate
local function GetString()
    for i = #numbers, 1, -1 do
        if numbers[i] == 0 then continue end
        local numberString = tostring(numbers[i])
        for j = i-1, 1, -1 do
            for k = 1, 3 - #tostring(math.floor(numbers[j])) do
                numberString = numberString .. "0"
            end
            numberString = numberString .. numbers[j]
        end
        return numberString
    end
end

local random = Random.new()
local value = 0
for i = 1, 1000000 do
    local r = random:NextNumber(10000000000, 1000000000000)
    Add(r)
    value += r
end

print(value)
print(GetValue())
print(GetAbbreviations())
print(GetString())
print(table.unpack(numbers))

in the second example we store the number in multiple variables so we get no errors


https://www.lua.org/pil/2.3.html

so i tried a random number and it given:
505040.79357498226Qi

on the GetAbbreviation()
everything else prints: 505123833102869584216.5

maybe you never added the extra numbers in the table?

I get the correct result let me show you

local abbreviations = {"", "K", "M", "B", "T", "Qa", "Qi", "S", "Sp", "O", "N", "D", "Und", "Dnd", "Trd", "Qad", "Qid", "Sid", "Spd", "Ocd", "Ncd", "V", "G", "Gp"}
local numbers = {}
for i, abbreviation in abbreviations do numbers[i] = 0 end

-- as long as you don't add very large numbers this should be accurate
local function Add(value)
	numbers[1] += value
	for i = 2, #numbers do
		local floor = math.floor(numbers[i-1] / 1000)
		numbers[i-1] -= floor * 1000
		numbers[i] += floor
	end
end

-- this might not be accurate as we are compressing all variables into one
local function GetValue()
	local value = 0
	for i = 1, #numbers do
		value += numbers[i] * 1000 ^ (i - 1)
	end
	return value
end

-- this might not be accurate as we are compressing all variables into one
local function GetAbbreviations()
	for i = #numbers, 1, -1 do
		if numbers[i] == 0 then continue end
		local value = 0
		local count = 0
		for j = i, 1, -1 do
			value += numbers[j] / (1000 ^ count)
			count += 1
		end
		return value .. abbreviations[i]
	end
end

-- this will be accurate
local function GetString()
	for i = #numbers, 1, -1 do
		if numbers[i] == 0 then continue end
		local numberString = tostring(numbers[i])
		for j = i-1, 1, -1 do
			for k = 1, 3 - #tostring(math.floor(numbers[j])) do
				numberString = numberString .. "0"
			end
			numberString = numberString .. numbers[j]
		end
		return numberString
	end
end

Add(505123833102869584216)

print(GetValue())
print(GetAbbreviations())
print(GetString())

image

five hundred five quintillion one hundred twenty-three quadrillion eight hundred thirty-three trillion one hundred two billion eight hundred sixty-nine million five hundred eighty-four thousand two hundred sixteen

i added more tables but it still dosent do 505SX

i also dont want long decimals .1238331029696Qi

and when i do VERY long number it printed:
6.378648563717437e+26Qi

you can do

local roundedValue = math.round(505.1238331029696)
print(roundedValue) -- this will print 505

or

local stringValue1 = string.format("%.2f", 505.1238331029696)
print(stringValue1) -- this will print 505.12

local stringValue2 = string.format("%.3f", 505.1238331029696)
print(stringValue2) -- this will print 505.123

local stringValue3 = string.format("%.4f", 505.1238331029696)
print(stringValue3) -- this will print 505.1238

thanks i now know alot of formats and i tried using another model and it worked perfectly fine