How to make number abbreviations?

So I tried making number abriviations but idk how to use them these are the tw function I used:

function abbreviateNumber(number)
	local roundDown = tostring(math.floor(number))
	return string.sub(roundDown, 1, ((#roundDown - 1) % 3) + 1) .. ({"", "", "", "B", "T", "QA", "QI", "SX", "SP", "OC", "NO", "DC", "UD", "DD", "TD", "QAD", "QID", "SXD", "SPD", "OCD", "NOD", "VG", "UVG"})[math.floor((#roundDown - 1) / 3) + 1]
end

function comma_value(amount)
	local formatted = amount
	while true do  
		formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
		if (k==0) then
			break
		end
	end
	return formatted
end

What I want to create is that the number will have commas like this 45,432,765 until 1 billion then I want it to be 1B or 1b.

Can someone please help me with it?

1 Like

How high can “number” be? Up to a trillion or beyond that?

idk forever it doesnt matter to me

I don’t think there is an automatic format for this so you have to do it manually. The quickest way I found was like so:

function abbreviateNumber(number)
	local digitsPassedBil = number / 1000000000
	local suffix = ""
	if digitsPassedBil >= 1 then
		suffix = "B"
	end	
	if digitsPassedBil >= 1000 then
		digitsPassedBil /= 1000
		suffix = "T"
	end
	if digitsPassedBil >= 1000 then
		digitsPassedBil /= 1000
		suffix = "q"
	end
	print(tostring(digitsPassedBil)..suffix)
	return tostring(digitsPassedBil)..suffix
end

Add if statements for more suffixes and only change the suffix in the if statement.

It is not working it givees the number something like it devides the number by 100 or 1000

function formatNumber(n)
    if n >= 10^6 then
        return string.format("%.2fm", n / 10^6)
    elseif n >= 10^3 then
        return string.format("%.2fk", n / 10^3)
    else
        return tostring(n)
    end
end

You have to add more values as you go higher. Ensure it’s ordered from highest to lowest. This example uses millions and thousands, with 2 decimal points.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.