How to make abbrevite numbers have decimals

So this script needs to make numbers be like that
1000 = 1k
1010 = 1.01k
1100 = 1.1k

But it only make it be 1k like
1100 = 1k
1010 = 1k
1XXX = 1k

So this is my script, if someone can help me and tell me how i can do that it will be helpful

local abbreviations = {
	"K", -- 4 digits 
	"M", -- 7 eigits
	"B", -- 10 digits
	"T", -- 13 digits
	"Q", -- 16 digits
	"Qi", -- 19 digits
	"S", -- 22 digits
	"Sx", -- 25 digits
	"Sp", -- 25 digits
	"Oc", -- 28 digits
	"N", -- 31 digits
	"De", -- 34 digits
	"Un", -- 37 digits
	"Tr", -- 40 digits
}

local function Abbreviate(x)
	if x < 1000 then 
		return tostring(x)
	end

	local digits = math.floor(math.log10(x)) + 1
	local index = math.min(#abbreviations, math.floor((digits - 1) / 3))
	local front = x / math.pow(10, index * 3)

	return string.format("%i%s", front, abbreviations[index])
end

:slight_smile: If you can tell me how to do that in a way that i will understand it will be good :smiley:

It might help you to understand I guess :wink:

local abbreviations  = {
	"K", -- 4 digits 
	"M", -- 7 eigits
	"B", -- 10 digits
	"T", -- 13 digits
	"Q", -- 16 digits
	"Qi", -- 19 digits
	"S", -- 22 digits
	"Sx", -- 25 digits
	"Sp", -- 25 digits
	"Oc", -- 28 digits
	"N", -- 31 digits
	"De", -- 34 digits
	"Un", -- 37 digits
	"Tr", -- 40 digits
}

local maxValue = 1000

function Abbreviate(value)
	local cut = maxValue
	if value  < cut then
		return value 
	else
		local inStr = tostring(math.floor(value ))
		if inStr:lower() == "inf" or inStr:find("nan") then
			return inStr
		end
		local length, str
		local isExp, strEnd = inStr:find("e%+")
		if isExp then
			length = inStr:sub(strEnd + 1) + 1
			str = tostring(inStr:sub(1, isExp - 1) * 100):sub(1, 3)
		else
			length = #inStr
			str = inStr:sub(1, 3)
		end
		local div, rem = math.floor(length / 3), length % 3
		if rem == 0 then
			div = div - 1
		end
		if div > #abbreviations  then
			print("div", div, "greater than suff", #abbreviations )
			str = inStr
		else
			if rem == 1 then
				str = tostring(tonumber(str) / 100)
			elseif rem == 2 then
				str = tostring(tonumber(str) / 10)
			end
			local suff = abbreviations [div]
			if suff == "O" then
				str = str .. " " .. suff
			else
				str = str .. suff
			end
		end
		return str
	end
end

print(Abbreviate(5110))

--// Output: 5.11K

Can it work for more than 1k? like 243.01B or something?

And Thx Btw :stuck_out_tongue:

Yes, It will go to “K” - “Tr”. If you add more in abbreviations = { } It will work as well.

Hm i tried, i tried to print(Abbreviate(192481241)) and it was just 192M in the output
:confused: thank you for helping me Btw

Try it urself maybe?
:slight_smile:
Ok try to print a number more than 1M

Isn’t 192481241 = 192M? There are only 9 numbers in a hundred million.

this function should work:

function Abbreviate(value)
	local abbreviations = {
		"K", -- 4 digits 
		"M", -- 7 eigits
		"B", -- 10 digits
		"T", -- 13 digits
		"Q", -- 16 digits
		"Qi", -- 19 digits
		"S", -- 22 digits
		"Sx", -- 25 digits
		"Sp", -- 25 digits
		"Oc", -- 28 digits
		"N", -- 31 digits
		"De", -- 34 digits
		"Un", -- 37 digits
		"Tr", -- 40 digits
	}
	local newstring = ""
	if value >= 1e+3 and value < 1e+6 then
		local args = string.split(tostring(value / 1e+3), "")
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[1]
	end
	if value >= 1e+6 and value < 1e+9 then
		local args = string.split(tostring(value / 1e+6), "")
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[2]
	end
	if value >= 1e+9 and value < 1e+12 then
		local args = string.split(tostring(value / 1e+9), "")
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[3]
	end
	if value >= 1e+12 and value < 1e+15 then
		local args = string.split(tostring(value / 1e+12), "")
		local isperiod = false
		local ready = false
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[4]
	end
	if value >= 1e+15 and value < 1e+18 then
		local args = string.split(tostring(value / 1e+15), "")
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[5]
	end
	if value >= 1e+18 and value < 1e+21 then
		local args = string.split(tostring(value / 1e+18), "")
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[6]
	end
	if value >= 1e+21 and value < 1e+24 then
		local args = string.split(tostring(value / 1e+21), "")
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[7]
	end
	if value >= 1e+24 and value < 1e+27 then
		local args = string.split(tostring(value / 1e+24), "")
		local isperiod = false
		local ready = false
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[8]
	end
	if value >= 1e+27 and value < 1e+30 then
		local args = string.split(tostring(value / 1e+27), "")
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[9]
	end
	if value >= 1e+30 and value < 1e+33 then
		local args = string.split(tostring(value / 1e+30), "")
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[10]
	end
	if value >= 1e+33 and value < 1e+36 then
		local args = string.split(tostring(value / 1e+30), "")
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[11]
	end
	if string.len(newstring) == 0 then
		newstring = tostring(value)
	end
	return newstring
end

Sorry that it’s so long and it was a little rushed too.
Also, if you need to test, add this as another line below the function:

print(Abbreviate(1.21e+3)) --should print 1.21K

it needed to be 192.48M… and it was just 192M

k i will try it now :stuck_out_tongue: Thx Btw

Yes!, it worked!
Thank you!!!