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

How do you round to 1-2 decimal places?

So I am currently new to scripting, and I'm making an obstacle course. I have a script here for the progress bar that displays the percentage:

local plr = game:GetService("Players").LocalPlayer

while wait() do script.Parent.Text = "Progress: " .. (plr.leaderstats.Stage.Value/28)*100 .. "%" end

I am wondering how to round this number up to 1 or 2 decimal places.

Thank you!

1 answer

✓ Accepted answer

There's no simple way to do this sadly but it is most definitely possible. First you need to move the decimal place. This can be done by multiplying it by 10, 100, 1000 but there is a more modular solution as well.

For all of the following replace Num with your unrounded variable, and replace # with how many decimal places you want to round by. (1 or 2 in your case)

Num * 10^(#)

Next you need to use math.floor to round down the number. One thing is that in most cases you will need to round up and down where math.floor only rounds down. To fix this simply add .5 to your number before rounding.

If you do want to only round down do not add the .5 and if you only want to only round up add 1 instead.

math.floor(Num * 10^(#) + 0.5)

Now you need to get it back to its original decimal place. To do this do the same as before and divide instead of multiplying

math.floor(Num * 10^(#) + 0.5) / 10^(#)

That should get you your rounded number.

1 comment

0

I just tested it, Thank you So Much!

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