I need help with large abbreviations!

I am working on a simulator game where your stats are abbreviated. I just have one problem. Yes, it works, but for some reason, when you go past above 99.9T, it starts glitching. It sometimes says 0.0QN or 9999999999999999999999K. I don’t know why it isn’t working, but I’ve tried everything! I changed from int value to number value, but I still get the same result! Here is what my abbreviation method looks like:

local data = game.ServerStorage.PlayerData
local abbreviations = {
    K = 4,
    M = 7,
    B = 10,
    T = 13,
    Q = 16,
    QN = 19,
    S = 22,
    SP = 25,
    O = 28,
    N = 31,
    D = 34,
    U = 37,
    DD = 40,
    TD = 43,
    QD = 46,
    QND = 49,
    SD = 52,
    SPD = 55,
    OD = 58,
    ND = 61,
    VT = 64,
    UVT = 67,
    DVT = 70,
    TVT = 73,
    QVT = 76,
    QNVT = 79,
    SVT = 82,
    SVPT = 85,
}

function ChangeValue(text, data, chosenAbbreviation)
    if chosenAbbreviation ~= nil then
        local digits = abbreviations[chosenAbbreviation]
        local rounded = math.floor(data.Value / 10 ^ (digits - 2)) * 10 ^ (digits - 2)
        text.Value = string.format("%.1f", rounded / 10 ^ (digits - 1)) … chosenAbbreviation
    else
        text.Value = data.Value
    end
end

game.Players.PlayerAdded:Connect(function(player)
    –// Data
    local playerData = Instance.new(“Folder”, data)
    local dataCash = Instance.new(“NumberValue”, playerData)
    dataCash.Name = “Cash”

    --// Leaderstats
    local cash = Instance.new("StringValue", leaderstats)
    cash.Name = "Cash"

    --// Abbreviation
    local chosenAbbreviation

    while wait() do
    	local cashText = tostring(math.floor(dataCash.Value))
    	
    	for abbreviation, digits in pairs(abbreviations) do
    		if #cashText >= digits and #cashText < (digits + 3) then
    			chosenAbbreviation = abbreviation
    			break
    		else
    			chosenAbbreviation = nil
    		end
    	end
    	ChangeValue(cash, dataCash, chosenCashAbbreviation)
    end
end)

I’m not sure why this keeps working incorrectly. Please help me!

1 Like

the part where it says ‘-/Data’ was a type that part isn’t actually the problem

Your suffixes can be a lot more simple than that, look at this article: Most efficient way of converting a number in scientific notation into a Suffix and back? - #3 by woot3

You can make it simply as this:

local suffixes = {'','K+','M+','B+','T+','qd+','Qn+','sx+','Sp+','O+','N+','de+','Ud+','DD+','tdD+','qdD+','QnD+','sxD+','SpD+','OcD+','NvD+','Vgn+','UVg+','DVg+','TVg+','qtV+','QnV+','SeV+','SPG+','OVG+','NVG+','TGN+','UTG+','DTG+','tsTG+','qtTG+','QnTG+','ssTG+','SpTG+','OcTG+','NoAG+','UnAG+','DuAG+','TeAG+','QdAG+','QnAG+','SxAG+','SpAG+','OcAG+','NvAG+','CT+'}
local function format(val)
	for i=1, #suffixes do
		if tonumber(val) < 10^(i*3) then
			return math.floor(val/((10^((i-1)*3))/100))/(100)..suffixes[i]
		end
	end
end

And then simply change the format like so:

script.Parent.Text = format(display2.Value)  -- path to Text, and then change the format of your value
8 Likes