Math format help

If there’s a post or site that explains this or perhaps it’s simpler than I though please let me know but I’m saving a number as an attribute on a button and I wish to format it correctly before setting it.

I have a cost calculating the cost of the button and it’s returning a number like 5464758643 and I want it to be like 5460000000 before setting it. How would I go about that.

5439 to be something like 5500
1200075 to be something like 1200000
5464758643 to be something like 5460000000

Just so it’s not just 5000, 100000, 500000000, etc.

1 Like
local function Format(n: number): number
	local sign = math.sign(n)
	n = math.abs(n)
	return (n - (n % 10 ^ math.floor(math.log(n, 10) - 1))) * sign
end

print(Format(67878798294232))

maybe this would be something you’d like

1 Like

To add on with more ideas:

local originalNumber = 89223589324
print(originalNumber)

local function Format(n: number): number
	
	local sign = math.sign(n)
	n = math.abs(n)
	return (n - (n % 10 ^ math.floor(math.log(n, 10) - 1))) * sign
	
end

local roundedNumber = Format(originalNumber)
print(roundedNumber)

local suffixTable = {
	
	{value = 1e12,letterSequence = "T"},
	{value = 1e9,letterSequence = "B"},
	{value = 1e6,letterSequence = "M"},
	{value = 1e3,letterSequence = "K"},
	
}

local function abbreviateNumber(number)
	
	for index,suffix in ipairs(suffixTable) do
		
		if number >= suffix.value then
			
			return string.format("%.2f%s",number / suffix.value,suffix.letterSequence)
			
		end
		
	end
	
	return tostring(number)
	
end

local abbreviatedNumber = abbreviateNumber(roundedNumber)
print(abbreviatedNumber)

local extraRoundedNumber = abbreviatedNumber:gsub("(%d+)%.?0*(%a*)$","%1%2")
print(extraRoundedNumber)

thank you for that both of you.

1 Like