How to make 1 000 000 become 1M in text

So I got a problem, I’m making a game where you can have money and all this stuff and when you have above 1M, it’s showing 1000000 and not 1M but I want 1M, 1B, 1T and everything above.

Can anyone help me?

4 Likes

Could you not split up the string up and then see how many letters/numbers are in the string.

3 Likes

You can use this code:

local abbreviator = {}

-- variables
local abbreviations = {
	["k"] = 4,
	["m"] = 7,
	["b"] = 10,
	["t"] = 13,
	["qd"] = 16,
	["qi"] = 19,
	["se"] = 22,
	["sp"] = 25,
	["o"] = 28,
}

-- functions
function abbreviator:Abbreviate(number)
	local text = tostring(string.format("%.f",math.floor(number)))

	local chosenAbbreviation
	for abbreviation,digit in pairs(abbreviations) do
		if (#text >= digit and #text < (digit + 3)) then
			chosenAbbreviation = abbreviation
			break
		end
	end
	
	if (chosenAbbreviation) then
		local digits = abbreviations[chosenAbbreviation]
		
		local rounded = math.floor(number / 10 ^  (digits - 2)) * 10 ^  (digits - 2)
		text = string.format("%.1f", rounded / 10 ^ (digits - 1)) .. chosenAbbreviation
	else
		text = number
	end
	
	return text
end

return abbreviator

(Code is in a module script)

Normally I wouldn’t give out code but this is extremely simple and you can follow this tutorial to understand (It’s really good): Abbreviate Numbers/Currency! - Roblox (Rounding and Formatting Strings) - YouTube

9 Likes

if you use values, use numbervalues instead of intvalues

1 Like

That’s a bit lengthy just for a number formatter. I would find this one better by all means:

function formatNumber(amount)
	local tableNo_ = tostring(amount):reverse():split("")
	local str = ""

	for i, c in ipairs(tableNo_) do
		str ..= c

		if i % 3 == 0 and i < #tableNo_ then
			str ..= ","
		end
	end

	return str:reverse()
end
2 Likes

OP was asking how to abbreviate numbers, not add commas to the zeros

1 Like

oml I totally misread it, my bad :skull:

1 Like