Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is this number rounding script not working correctly?

Asked by 8 years ago

The script is supposed to round a number to the nearest hundredths place. However, it rounds to a whole number, and sometimes that whole number is incorrect.

Can someone please help me?

Here's the code:

local Player = game.Players.LocalPlayer
local Stats = Player.leaderstats

while wait(0.1) do
    if Player ~= nil then
        if Stats ~= nil then
            local Number = math.floor((Stats.Kills.Value/math.max(1,Stats.Deaths.Value)))
            function round(x,near)
                return math.floor(x/near+.5)*near
            end
            wait(0.1)
            script.Parent.Text = "Ratio: "..round(Number,2)..""
        end
    end
end

1 answer

Log in to vote
1
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
8 years ago

It's rounding to the nearest 2. You want to round to the nearest .01

0
Well then. I also asked on the Scripters forum on ROBLOX and the person that helped me out there used non-decimals CoolJohnnyboy 121 — 8y
1
math.floor(x*100+.5)/100 also rounds to the nearest hundredth. 1waffle1 2908 — 8y
0
It still isn't working for me. Might it possibly be that I'm trying to project the results in GUI in a text box? CoolJohnnyboy 121 — 8y
1
You're already rounding down on line 7. 1waffle1 2908 — 8y
0
Now it works. Thanks! CoolJohnnyboy 121 — 8y
Ad

Answer this question