Unable to go beyond 999.99b with abbreviation

Hi!

For a few days now, I have ran into an issue where I needed to set a max limit of 999.99b because once I go past that, the converting function becomes a bit weird.
The issue is that I’m unable to get numbers like 999.99t and 1t (it becomes 1000t and 1000b), and so on.
Was wondering if anyone could look into this for me as I could really use some help on this issue.
I was originally planning to go as high as 10^30 (or even higher for that matter) without it being formatted incorrectly.

Thank you!!

function Functions:AbbreviateNumber(Number)
	local Abbreviations = {
		{10^30, "o"},
		{10^27, "sp"},
		{10^24, "sx"},
		{10^21, "qn"},
		{10^18, "qd"},
		{10^15, "t"},
		{10^9, "b"},
		{10^6, "m"},
		{10^3, "k"}
	}

	for _, abbr in ipairs(Abbreviations) do
		if Number >= abbr[1] then
			local formattedNumber = string.format("%.2f", math.floor(Number / (abbr[1] / 100)) / 100)
			formattedNumber = string.gsub(formattedNumber, "%.?0+$", "") -- // Remove trailing zeros
			return formattedNumber .. abbr[2]
		end
	end

	return tostring(Number)
end
2 Likes

This might help, it goes up to 10^69:

1 Like

This does not include commas, which it the entire issue with that post.
I rely on two decimals to be shown when needed too.

I think I found the issue, try using math.round() instead of math.floor

function Functions:AbbreviateNumber(Number)
	local Abbreviations = {
		{10^30, "o"},
		{10^27, "sp"},
		{10^24, "sx"},
		{10^21, "qn"},
		{10^18, "qd"},
		{10^15, "t"},
		{10^9, "b"},
		{10^6, "m"},
		{10^3, "k"}
	}

	for _, abbr in ipairs(Abbreviations) do
		if Number >= abbr[1] then
			local formattedNumber = string.format("%.2f", math.round(Number / (abbr[1] / 100)) / 100)
			formattedNumber = string.gsub(formattedNumber, "%.?0+$", "") -- // Remove trailing zeros
			return formattedNumber .. abbr[2]
		end
	end

	return tostring(Number)
end

No, that just rounds it up, which means 9999 will turn into 10k instead of 9.99k.