How to round huge numbers?

hey guys, I have a question. when working with extremely huge numbers, is there a special way to round the decimals? I cant figure out a way to do this. for example if i had a number in the trillions, EX: 1,234,567,890,654, with my script it would pop up as 1.234567890654T. Instead i would like it to pop nice and clean as 1.2T. how could this be done and implemented into my current script? (Script Below)

Coins.Changed:Connect(function()
	if Coins.Value >=10^6 and Coins.Value < 10^9 then
		script.Parent.CoinFrame.Amount.Text = Coins.Value/10^6 .. "M" 
	elseif Coins.Value >= 10^9 and Coins.Value <10^12 then            
		script.Parent.CoinFrame.Amount.Text = Coins.Value/10^9 .. "B"
	elseif Coins.Value >= 10^12 and Coins.Value <10^15 then
		script.Parent.CoinFrame.Amount.Text = Coins.Value/10^12 .. "T"
	elseif Coins.Value >= 10^15 and Coins.Value <10^18 then
		script.Parent.CoinFrame.Amount.Text = Coins.Value/10^15 .. "Q"
	end
end)
1 Like
function roundNumber(num, numDecimalPlaces)
  return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
end

Coins.Changed:Connect(function()
	if Coins.Value >=10^6 and Coins.Value < 10^9 then
		script.Parent.CoinFrame.Amount.Text = roundNumber(Coins.Value/10^6, 1)  .. "M" 
	elseif Coins.Value >= 10^9 and Coins.Value <10^12 then            
		script.Parent.CoinFrame.Amount.Text = roundNumber(Coins.Value/10^9 , 1) .. "B"
	elseif Coins.Value >= 10^12 and Coins.Value <10^15 then
		script.Parent.CoinFrame.Amount.Text = roundNumber(Coins.Value/10^12, 1)  .. "T"
	elseif Coins.Value >= 10^15 and Coins.Value <10^18 then
		script.Parent.CoinFrame.Amount.Text = roundNumber(Coins.Value/10^15, 1) .. "Q"
	end
end)
1 Like

It works perfectly… but I’m so confused. What is string.format the percent sign and all this stuff? (if you dont mind explaining)

tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
the % in the string.format is just a format specifier, so it starts with that
then after it is the . and number which is the precision and when you use it with the “f” it will print the number of digits after the .
tonumber() then converts the string to a number
if you want to read more string | Documentation - Roblox Creator Hub

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