Rounding values to the nearest one in a table

Im creating a graphing calculator and i want to zoom in and out

My issue is i want the graph to increment by 1, 2, or 5s (and then multiply by a factor of 10 for increments of 100, 200, 500, 1000, 2000, etc).

I’ve thought of a way and it looks something like this

local t = {1,2,5}

function round(x)
--code i dont know how to go about making
end
round(7) -- rounds down to 5
round(2) -- stays at 2
round(1.5) -- you get the idea, rounds down to 1

any help/ guidance is appreciated

That wasn’t my issue. I wanted to round numbers down to the nearest value in a table

eg:
I have a table of {1,2,5}

7 would round to 5
4 would round to 2
and 1.1 would round to 1

local t = {1,2,5}

function round(x)
  if x > t[3] then return t[3]
  elseif x>t[2] then return t[2]
  else return t[1]
end
round(7) -- rounds down to 5
round(2) -- stays at 2
round(1.5) -- you get the idea, rounds down to 1
2 Likes

mAth .Floor or math.round

local function Round(Number, Increment)
   return math.floor(Number + Increment)
end

Round(7, 5) -- rounds down to 5
Round(2, 2) -- stays at 2
Round(1.5, 1) -- you get the idea, rounds down to 1

The first way I thought of was to just loop through the table and check which value is closest to x. The code would be something like this (this assumes that t is sorted in ascending order).

local t = {1,2,5}

function round(x)
    local low = nil
    local high = nil
    for i, round_value in ipairs(t) do
        if round_value > x then
            high = round_value
            low = t[i-1]
            break
        end
    end
    -- Case where x is larger than all t values
    if not high then return t[-1] end
    -- Case where x is smaller than all t values
    if not low then return high end
    return (math.abs(x - low) < math.abs(x - high)) and low or high
end
round(7) -- rounds down to 5
round(2) -- stays at 2
round(1.5) -- you get the idea, rounds down to 1

This is wrong because it doesn’t round down. It would work with some tweaks though which hopefully you can figure out.

I assume all of the values in the table are in ascending order?

local function round(x)
    for i = 1, #t do -- Iterate over the table
        if t[i] > x then -- Keep looping until t[i] is higher than x, meaning the previous element in the table (t[i-1]) is less than x. x will be between the two numbers.
            if i > 1 then -- Or, if we put 0 into the table, t[i-1] will be nil because i will be 1. This ensures that we return the lowest result rather than trying to compare to a nil number.
                if t[i] - x > x - t[i-1] then -- Compare the distance between the higher number and x, and the lower number and x.
                    return t[i-1] -- If the lower number is closer to x, return the lower number.
                else
                    return t[i] -- If the higher number is closer to x, return the higher number.
                end
            else
                return t[i] -- If x is smaller than the first number in the table, return the first number in the table.
            end
        end
    end
    return t[#t] -- The loop finished, meaning x was larger than anything in the table. Return the largest number in the table.
end
          

He’s not trying to round an integer. He’s trying to get the closest number from a table. Double-check the original post.

1 Like
local t = {1,2,5}


function round(x)
	for i=#t, 1, -1 do
		if x >= t[i] then
			return t[i]
		end
	end
end
round(7)  -- rounds down to 5
round(2)  -- stays at 2
round(1.5)  -- you get the idea, rounds down to 1

In addition, you can use table.sort to sort a table in ascending order

1 Like

I assume they were trying to round down to the nearest value

local function RoundToNearestTableValue(Table, Number)
	table.sort(Table)
	local ClosestValue, ClosestDifference = nil, math.huge
	for Index, Value in ipairs(Table) do
		local Difference = math.abs(Number - Value)
		if Difference > ClosestDifference then continue end
		ClosestDifference = Difference
		ClosestValue = Value
	end
	return ClosestValue
end

local Result = RoundToNearestTableValue({1, 5, 9}, 6)
print(Result) --5

This can be cut down even further.

local function RoundToNearestTableValue(Table, Number)
	table.sort(Table, function(Left, Right) return math.abs(Number - Left) < math.abs(Number - Right) end)
	return Table[1]
end

local Result = RoundToNearestTableValue({1, 5, 9}, math.pi)
print(Result) --5
3 Likes