Trying to create a abbreviation script for number

local abb = {
	['K'] = 4,
	['M'] = 7,
	['B'] = 10,
	['Qa'] = 13
}


local abbreviatedNum = {}

local function abbre (number)
	local text = tostring(math.floor(number))
	if #text > 3 then
		for i,abb in pairs(abb) do
			if abb == #text or #text < (abb + 3) then
				for abb in text:gmatch(".") do
					table.insert(abbreviatedNum,abb)
				end
				print(abbreviatedNum[1] .. '.' .. abbreviatedNum[2].. i)
				return
			end
		end
		print(#abbreviatedNum)
	end
end


abbre(2250)


For some reason the above code print 2.2M and not 2.2K ,

There are no errors in the console

how do I fix this??

Use and. The is because when looping through a table, and using a condition/s, the value returned will be the value that has satisfied the condition. Since or evaluates as true if either condition is true, and your second condition is true, abbs would be that returned value.

Basically:

length of number = 4
#text < (length of number + 3) : true
since this condition is true, and or evaluate as true if either condition is true, return (abb+3), hence abbs is 7

If you used and, only the first condition can evaluate to true. Also, I don’t think you need a second condition since that condition does the same thing as the first, just differently.

1 Like

Thanks it works now :))))) and I can finally move onto other scripts