Rounding numbers to the nearest hundredth

So if I have 0.41506968 it rounds to the nearest hundredth which would be 0.4

1 Like

You should look into math.floor.

Here is an example of a function that uses math.floor, to round numbers to a desired place.
https://devforum.roblox.com/t/rounding-numbers/13603/3?u=excessenergy

Also, in your example, 0.4 would be rounding to the tenths place, not the hundredths.

2 Likes

Please check to see if your question has been asked before posting. There are about 7 other topics with the same question.

Look up “How to round numbers” on the devforum search bar.

2 Likes

They all show how to round to whole numbers

1 Like

There’s now a math.round function but it goes to the nearest integer.

For other cases you might have to implement your own function:

local function round(x, degree, base)
	base = base or 10 -- default
	degree = degree or 0 -- default
	local n = base ^ degree
	return math.floor(x * n + 0.5) / n
end

print(round(0.41506968, 2)) --> 0.42
1 Like

The function from the link I posted has a numPlaces parameter, that allows you to input the number of places you want to keep.

2 Likes

how do I make it so it always rounds down?

local function floor(x)
		return x - x % 1
end

local Timee = floor(Time * (10^2)) / (10^2)

1 Like

math.floor always rounds down from 0.5. If you wanted to round up you would have to use math.ceil.

2 Likes