Rounding a number to step
Rounding integers to a given step is a very common task, often it is asked at technical interviews and very often such a task appears in daily jobs.
Algorithm
- (optional) Subtract
offsetfromx;text x = x - offset - divide step #1 value by
step;text x = x / step - (optional) to make it work like a round function, then add to the step #3 value
0.5;text x = x + 0.5 - apply
math.ceilfunction to step #3 value,text x = ceil(x) - multiply step #2 value by
step;text x = x * 2 - (optional) After adding an
offsetto the final result.text x = x + offset
Function for the rounding
---@param n number
---@param step number
---@param offset? number is added to the result
local function round(x, step, offset)
offset = offset or 0
return math.ceil((x - offset) / step + 0.5) * step + offset
end
Examples
print(round(-643, 50)) --> -650
print(round(-117, 10, 10)) --> -120
print(round(-113, 10)) --> -110
print(round(0, 10)) --> 0
print(round(9, 10)) --> 10
print(round(10, 10)) --> 10
print(round(133, 20)) --> 140
print(round(122, 20)) --> 120
print(round(134, 50)) --> 150
print(round(1123, 50)) -->1100
