Hello there,
I am wondering how you could round up/down values to one decimal? This is my script for a progress bar showing how many percentages you have completed in an obstacle course so far.
01 | --// Variables |
02 |
03 | local Players = game:GetService( "Players" ) |
04 | local Player = Players.LocalPlayer |
05 |
06 | local Leaderstats = Player:WaitForChild( "leaderstats" ) |
07 | local ProgressText = script.Parent |
08 | local MaxStages = 101 -- Whatever my current highest stage is. |
09 |
10 | --// Progress Percentage Function |
11 |
12 | while wait() do |
13 | ProgressText.Text = ( "Progress: " ..Leaderstats.Stage.Value* 100 /MaxStages.. " %" ) |
14 | end |
This works fine, although depending on how many stages I do have, I'll get an absurd amount of decimals. So I am wondering how you could round up/down the value to only give the player one decimal?
I would love to see an example of what I could do? Thank you.
Let's say you have the number 45.45783, and you are trying to round it to 45.5:
You would move the decimal place to the right one time by multiplying it by 10.
1 | local number = 45.45783 |
2 |
3 |
4 | local newNumber = 45.45783 * 10 |
5 | -- 454.5783 |
Then add .5 to it, because 0 - 4 rounds down, and 5 - 9 rounds up
1 | local anotherNumber = newNumber + 0.5 |
2 | -- 455.0783 |
Now just do math.floor:
1 | local flooredNumber = math.floor(anotherNumber) |
2 | -- 455 |
Then, divided it 10 to go back the original decimal place.
1 | local roundedNumber = flooredNumber/ 10 |
2 | -- 45.5 |
Here is all the codes combined:
1 | local number = 45.45783 |
2 |
3 | local roundedNumber = (math.floor((number * 10 ) + 0.5 ))/ 10 |
To put that in your situation:
1 | local stageNumber = Leaderstats.Stage.Value/MaxStages |
2 |
3 | ProgressText.Text = "Progress: " .. ((math.floor((stageNumber * 10 ) + 0.5 ))/ 10 ) .. "%" |