Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i round numbers after a comma???

so im making a single player tycoon game and im adding cash suffixes. I mean like when you have 1 000 000 money, instead of displaying 1 000 000 its gonna show you 1M.

Everything works perfectly, but when i get a number like 1 839 918, instead of 1.83M I get 1.839918M. Is there any way to round numbers after a comma?

My english isnt really good but i hope you understand my issue. Heres the script, i dont know if its gonna help but ima just put it here:

local Money = script.Parent.Money -- text gui which displays the money
while true do
	wait()
	local playersmoney = game.Players.LocalPlayer.leaderstats.Cash.Value -- players money
	if playersmoney > 0 and playersmoney < 1000 then
		Money.Text = playersmoney
	elseif playersmoney > 999 and playersmoney < 1000000 then
		Money.Text = playersmoney / 1000 .."K"
	elseif playersmoney > 999999 and playersmoney < 1000000000 then
		Money.Text = playersmoney / 1000000 .."M"
	end
end

2 answers

✓ Accepted answer

Hello!

This is one of the easiest methods to do it.

I spent some time making the syntaxes but it is very easy to set up with other reasons aswell.

function abb(n)
    if n>=1*10^4 and n<1*10^6 then
        n=(n/1*10^-3)
        return string.sub(tostring(n),1,5).."K"
    elseif n>=1*10^6 and n<1*10^7 then
        n=(n/1*10^-6)
        return string.sub(tostring(n),1,3).."M"
    elseif n>=1*10^7 and n<1*10^8 then
        n=(n/1*10^-7)
        return string.sub(tostring(n),1,3).."B"
    elseif n>=1*10^8 and n<1*10^9 then
        n=(n/1*10^-8)
        return string.sub(tostring(n),1,3).."T"
    else
        return n
    end
end


	  
local MoneyGui = script.Parent.Money -- text gui which displays the money
wait(2)
local playersmoneystat = game.Players.LocalPlayer.leaderstats.Cash -- players money stats

playersmoneystat.Changed:Connect(function(money) --runs when cash value is changed
	MoneyGui.Text = abb(money) --sets the gui text
end)

6 comments

1

Thanks!

1

this version works, but it displays 14.5M as 1.4B, doesnt display T, and probably some more things

0

That shouldn't happen, I probably made some mistakes. I'll take a look.

1

Ok

1

the suffixes are just kinda glitchy

1

ok i fixed it myself

You can use the math.floor() function for this, though you'll have to manipulate what you feed the function a bit:

local function Round(number, roundTo, canRoundUp)
	-- First get the value we're rounding
	local remainder = number % roundTo -- x % y gives the remainder of x / y
	if not canRoundUp then
		return number - remainder -- Since remainder < roundTo
	else
		local rounded = remainder * 2 -- Any number < roundTo/2 won’t pass the next mark
		rounded = rounded - (rounded % roundTo) -- Subtract what's "extra"
		return number + remainder
	end
end

Implementing this in your script:

local Money = script.Parent.Money

while true do
	wait()
	local playersmoney = game.Players.LocalPlayer.leaderstats.Cash.Value -- players money
	if playersmoney > 0 and playersmoney < 1000 then
		Money.Text = playersmoney
	elseif playersmoney > 999 and playersmoney < 1000000 then
		Money.Text = playersmoney / 1000 .."K"
	elseif playersmoney > 999999 and playersmoney < 1000000000 then
		Money.Text = Round(playersmoney , 10000)/ 1000000 .."M"
	end
end

P.S. Instead of using a while loop, you should be listening for changes, so you need to update only when a change is seen:

local Money = script.Parent.Money
local Cash =  game.Players.LocalPlayer.leaderstats.Cash

Cash.Changed:Connect(function()
	local playersmoney =  Cash.Value -- players money
	if playersmoney > 0 and playersmoney < 1000 then
		Money.Text = playersmoney
	elseif playersmoney > 999 and playersmoney < 1000000 then
		Money.Text = playersmoney / 1000 .."K"
	elseif playersmoney > 999999 and playersmoney < 1000000000 then
		Money.Text = Round(playersmoney , 10000)/ 1000000 .."M"
	end
end

2 comments

0

tbh i dont understand a single thing in the function that is supposed to round up lol

0

Take an example, if I want to round 17 to the nearest ten (which means we can round up). First I’m the remainder is 17 % 7 = 7. Now we add half of the remainder to itself so it gets above the nearest ten 7 + 3.5 = 10.5. Now we subtract the remainder of 10.5 % 10 which I gives us 10. Hence we add 10 to 10 = 20. Which is the rounded answer

This discussion is preserved as it was. New answers, comments, and votes are closed.