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

How to roundify numbers?

Asked by 4 years ago

I currently have this part of the script that awards the winner points and xp based on how much time the round had left, however if the round ends with a time left of like 264, the winner will be rewarded 26.4 points and 8.2 xp. How could I remove the decimals from the numbers?

hidepointer = game.ReplicatedStorage.MapTimeTotal.Value - game.ReplicatedStorage.MapTimeLeft.Value
hidepoints = hidepointer / 10
hidexp = hidepoints / 2
                        game.ReplicatedStorage.PlayerData:FindFirstChild(v.Name).Points.Value = game.ReplicatedStorage.PlayerData:FindFirstChild(v.Name).Points.Value + hidepoints
                        game.ReplicatedStorage.PlayerData:FindFirstChild(v.Name).XP.Value = game.ReplicatedStorage.PlayerData:FindFirstChild(v.Name).XP.Value + hidexp

2 answers

Log in to vote
1
Answered by 4 years ago

I think what you're looking for is math.floor().

math.floor() returns the integer version of the given floating-point number (decimal). That decimal is truncated by math.floor(), which means that if, say, the decimal was 16.7, it would round down to 16.

There is another function of the math library called math.ceil() which, by looking at its name, you can tell rounds a floating-point number up instead of down. 16.x rounds up to 17, where x is either any digit except 0 or any number of digits except 0.

Back to math.floor(). If you want to round up with this function instead of math.ceil(), you would have to add the number being floored by 0.5:

local number = 3.4
print(math.floor(number + 0.5)) -- Prints 3

Do note, however, that this ensures proper rounding with the function. This means that 3.4 rounds to 3 but 3.7 rounds to 4.

Source

Ad
Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

You could use this little function if you want to round values.

function round(x)
    return math.floor(x + .5)
end

But if you want to remove decimals you can simply use math.floor(x) which removes all decimals.

Answer this question