How to achieve this number effect?

So I was wondering, how could I achieve an effect for when the number gets high it simplifies itself. This is for a currency display. I once created a script that does this, I was just wondering if there was a more efficient way, after all, it was like 70 lines, AND IT WAS JUST TO FORMAT THE NUMBER! Here are some examples of what I mean.

put in | what shows
1000 | 1000
10000 | 10,000
100000 | 100,000
1000000 | 1M
1200000 | 1.2M
12200000 | 12.2M
12000000 | 12M
120000000 | 120M
120200000 | 120M

This is for a TextLabel

Try looking at this.

1 Like

Do you know any not code ways? I thought I found on once.

Unfortunately I do not know any method that doesn’t require code to simply big numbers.

Thanks for trying to help @BIULDERBRO

Here is the code that I created that is overcomplicated. (This was about a year ago)

local money = script.Parent
local coins = money.Coins
wait()
local function changed ()
	money.Text = money.Coins.Value
	if coins.Value > 9999 and coins.Value < 1000000 then
		local number = money.Text
		local characters = string.len(tostring(number))
		local oldstring = ""
		if characters > 4 then
			local commanumber = math.floor((characters-1)/3)
			local leftover = characters - commanumber*3
			local beginstring = string.sub(tostring(number), 1, leftover)
			oldstring = beginstring
			local subtractnumber = 1
			if leftover == 1 then
				subtractnumber = 1
			elseif leftover == 2 then
				subtractnumber = 0
			elseif leftover == 3 then
				subtractnumber = -1
			end
			for i = 1,commanumber do
				oldstring = oldstring..","..string.sub(tostring(number), i*3-subtractnumber, i*3-subtractnumber+2)
			end
		else
			oldstring = tostring(number)
		end
		money.Text = oldstring
	elseif coins.Value > 999999 and coins.Value < 1000000000 then
		local finalstring
		if coins.Value < 10000000 then
			finalstring = string.sub(tostring(coins.Value), 1, 1).."."..string.sub(tostring(coins.Value), 2, 3)
		elseif coins.Value > 9999999 and coins.Value < 100000000 then
			finalstring = string.sub(tostring(coins.Value), 1, 2).."."..string.sub(tostring(coins.Value), 3, 3)
		elseif coins.Value > 99999999 then
			finalstring = string.sub(tostring(coins.Value), 1, 3)
		end
		if string.sub(finalstring, 4, 4) == "0" then
			finalstring = string.sub(finalstring, 1, 3)
			if string.sub(finalstring, 3, 3) == "0" or "." then
				finalstring = string.sub(finalstring, 1, 2)
				if string.sub(finalstring, 2, 2) == "." then
					finalstring = string.sub(finalstring, 1, 1)
				end
			end
		end
		money.Text = finalstring.."M"
	elseif coins.Value > 999999999 then
		local finalstring
		if coins.Value < 10000000000 then
			finalstring = string.sub(tostring(coins.Value), 1, 1).."."..string.sub(tostring(coins.Value), 2, 3)
		elseif coins.Value > 9999999999 and coins.Value < 100000000000 then
			finalstring = string.sub(tostring(coins.Value), 1, 2).."."..string.sub(tostring(coins.Value), 3, 3)
		elseif coins.Value > 99999999999 then
			finalstring = string.sub(tostring(coins.Value), 1, string.len(tostring(coins.Value))-9)
		end
		if coins.Value < 100000000000 then
			if string.sub(finalstring, 4, 4) == "0" then
				finalstring = string.sub(finalstring, 1, 3)
				if string.sub(finalstring, 3, 3) == "0" or "." then
					finalstring = string.sub(finalstring, 1, 2)
					if string.sub(finalstring, 2, 2) == "." then
						finalstring = string.sub(finalstring, 1, 1)
					end
				end
			end
		end
		money.Text = finalstring.."B"
	end
end

changed()
coins.Changed:Connect(changed)

Maybe you could simplify this instead?

1 Like

Does this work?

local money = script.Parent
local coins = money.Coins

local function formatNumber(value)
    if value >= 1e9 then
        return string.format("%.1fB", value / 1e9)
    elseif value >= 1e6 then
        return string.format("%.1fM", value / 1e6)
    elseif value >= 1e4 then
        return tostring(value):reverse():gsub("(%d%d%d)", "%1,"):reverse():gsub("^,", "")
    else
        return tostring(value)
    end
end

local function updateText()
    money.Text = formatNumber(coins.Value)
end

updateText()
coins.Changed:Connect(updateText)

If not let me know!

1 Like

Thanks @rocketman392 this is exactly what I’m looking for!

1 Like

No problem! Glad I could be of service!

1 Like

Tweaked it a bit for my needs. I probably don’t need all those types…

local order = {"M", "B", "T", "Qa", "Qi", "Sx", "Sp", "Oc", "No", "Dd", "Ud", "Dd", "Td", "Qad", "Qid", 
	"Sxd", "Spd", "Ocd", "Nod", "Vg", "Uvg", "Dvg", "Tvg", "Qavg", "Qivg", "Sxvg", "Spvg", "Ocvg"}

local function formatNumber(value: number)
	if value >= 1e6 then
		local index = math.log(value, 1000)//1 - 1
		local div = value/(10^((index + 1)*3))
		local numbertype = (math.log10(value)%3 >= 2 or math.round(div*10)/10 - math.round(div) == 0) and "d" or "f"
		return string.format("%.1"..numbertype..order[index], div)
	elseif value >= 1e4 then
		return tostring(value):reverse():gsub("(%d%d%d)", "%1,"):reverse():gsub("^,", "")
	else
		return tostring(value)
	end
end

(Got the list from @NyrionDev from @BIULDERBRO )

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