Abrevation not working

I tried to make a script which turns 1500 to 1.5k and 1500000 to 1.5 million

It works for 1.5k but does not work for 1.5 mil

local abbreivations = {
    ["T"] = 10^12,
    ["B"] = 10^9,
    ["M"] = 10^6,
    ["K"] = 10^3
}

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        if tonumber(msg) ~= nil then
            local chosenabrevation = nil
            local number = 0
            for i,v in pairs(abbreivations) do
                print(i.." is "..v)
                if tonumber(msg) > v then
                    chosenabrevation = i
                    number = tonumber(msg)/v
                end
            end
            if chosenabrevation ~= nil then
                print(number..chosenabrevation)
            else
                print(tonumber(msg))
            end
        end
    end)
end)

-- Player chatted number 1500, printed as 1.5k
-- Player chatted number 1500000, printed as 1500K not as 1.5 mil

Please tell how to fix this

sorry thats way too complicated, but can you tell me what makes my script not work?

All you have to do is type AbbreviateNumber(1000) with my function?

No, i am asking why the script i posted does not work

Probably because you’re supposed to use string.sub or you’re not diving the value enough

if tonumber(msg) > v then
				chosenabrevation = i
				number = tonumber(msg)/v
			end

you’re changing the abbreviation and dividing the number even though the value could be 6 digits but you also divide it by 3

for example 1 million would divide by 1E3 and 1E6 if I’m correct

you should do this but it will still return massive numbers for example 1.5671 million :confused:

I suggest you add a string.sub

local abbreivations = {
	["T"] = 10^12,
	["B"] = 10^9,
	["M"] = 10^6,
	["K"] = 10^3
}

local function A(msg)
	if tonumber(msg) ~= nil then
		local chosenabrevation = nil
		local number = 0
		for i,v in pairs(abbreivations) do
			if tonumber(msg) >= v and tonumber(msg) < v * 10 ^ 3 then -- or just v * 1000
				chosenabrevation = i
				number = tonumber(msg)/v
			end
		end
		if chosenabrevation ~= nil then
			print(number..chosenabrevation)
		else
			print(tonumber(msg))
		end
	end
end