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

Rounding a number to the nearest 3?

Is it possible to round a number to the nearest 3? like

8.999 -> 9

2.5 -> 3

11.9 -> 12

I cant find this kinda thing online

Lets say inside a script I have an int string the int strings value is 58 The script would round this to the nearest 3 (57)

of if the int strings value was 58.5 it would round to nearest 3 [This is in the middle of two 3s so it would be rounded up (60)]

Thanks to everybody who answered ( I shouldve explained better) but I got an answer now.

5 comments

0

Do you mean like round up to the nearest whole number?

0

Please edit your question to be more specific about what you actually mean. Your examples aren't really clear as to what you're looking for.

0

Are you sure you know what you're talking about? When rounded, 58 never becomes 57. It doesn't matter what kind of rounding you're doing.

0

I see what you mean now, but rounding isn't the correct terminology.

0

Let me edit my answer.

3 answers

✓ Accepted answer

It's actually extremely simple. Simply floor the number while dividing it by 3, add a half to it to round it (0.5), and then multiply that outcome by 3 again.

In code it'd look like this. math.floor(Number/3+0.5)*3

Eg. print(math.floor(5.4/3+0.5)*3) -> 6

print(math.floor(4.7/3+0.5)*3) -> 6

print(math.floor(4.2/3+0.5)*3) -> 3

Ps. if you prefer math.ceil, you can do the same thing, just change +0.5 to -0.5

1 comment

0

hi

You can easily do this using string.format. In your case, you can use zero as the argument for the n parameter Here's an example implementation:

local function roundToNPlaces(number, n)
	local roundedNumber = tonumber(string.format("%."..n.."f", number))
	if roundedNumber then
		return roundedNumber
	else
		return 0
	end
end

After your edits, I'm able to see what you mean. In future, try to better phrase your questions so that people understand (the examples especially made it confusing).


While it's true that you might not be able to find this stuff for Lua, if you look it up, there are actually many websites (this website is an example) with the simple solution:

-- if you want to find the closest above
print(3*math.ceil(13/3))
-- if you want to find the closest below
print(3*math.floor(13/3))
-- if you want to find the closest in general
print(3*math.floor(13/3 + 0.5))

####I hope this helps. Have a great day scripting!


Modulus


Well, the easiest way of doing this would be using the modulus operator, which essentially gets the remainder of quotient of the number before it and the number after it.

For example:

9%4

would return 1 as 4 goes into 9 twice, 4*2 = 8, and 9-8 is 1

An example of this would be:

function RoundToNumber (num,roundNum)
	if num%roundNum  >= roundNum/2 then
		return num - num%roundNum + roundNum
	else
		return num - num%roundNum
	end
end

this would round to the nearest provided roundNum.

It essentially rounds up if the remainder of quotient of the number and the number you want to round to is greater than half of the number you want to round to.


Visualizing with numbers


By adapting a more general function like that, we can get this result:

function RoundTo3 (num)
	if num%3  >= 3/2 then
		return num - num%3 + 3--round up
	else
		return num - num%3--round down
	end
end

Hopefully now you can visualize it more clearly.

The reason that we use modulo here is that it lets us get to a number between 0 and 3, which then can help us deduce whether it is closer to the upper multiple of 3, (result >=1.5), or the lower multiple of 3 (result < 1.5)

An example of this function would be:

RoundTo3(67)

as 67%3 is 1 (as the multiple of 3 below it is 66), it rounds down to 66:

so: the function returns 67 - 67%3, or 67 -1, which is 66

Another example would be:

RoundTo3(23)

as 23%3 is 2 (as the multiple of 3 below it is 21), it rounds up to 24:

so: the function returns 23 - 23%3 + 3 = 23 - 2 + 3 = 21 + 3 which is 24

Hopefully this helped, be sure to accept if it did!

11 comments

0

hmm I understand what your doing but I dont understand how I would put it into like lets say Im taking the value of a bool string thats inside the script, how would I implement it

0

can you show me an example?

0

I'm not sure what you mean by a "bool string", or how that pertains to rounding

0

^ maybe try editing your question to be more specific.

0

Lol an int string sorry

0

Have you heard of tonumber, or are you referring to something else?

0

yea, there is a built in tonumber() function in lua

0

I dont know what a tonumer is

0

its a typo, and number/int strings as you call them can be used in arithmetic operations(+,-,/,*,^,%,...) without being converted into a number

0

tonumber() converts a number as a string to a number, such as tonumber("5"), which returns 5

0

that was a type i meant to say an int value

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