How to find the next multiple of 5 of a number?

Hello. I am currently writing a script for my vehicle which will enable cruise control. I have gotten to the point where the cruise is able to be set up and down, however, I have only been able to do it by a certain amount each time.

What I want to happen, is for the cruise control speed (let’s say that it’s set at 17 MPH) to go up to the next multiple of 5 (20 MPH in this case). Same thing when setting down. (15 MPH in this case).

Any help is greatly appreciated.

Thanks!

You can use the % modulo operator, which gets the remainder of a number when dividing two numbers.

local multiple = 5
local function isMultiple(number)
	return number % multiple == 0
end

print(isMultiple(5)) -- prints true
print(isMultiple(25)) -- prints true
print(isMultiple(14)) -- prints false
1 Like

So how would I get the value of the remainder to subtract or add it to the setSpeed variable?

You can simply just use the formula (number % multiple) itself and not checking if it equals to 0. For example:

local multiple = 6
local number = 3
local remainder = number % multiple
print(remainder) -- prints 3
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.