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
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)
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