What does the % symbol do

Everybody knows what +, -, * and / do but what does % do…
There’s almost NO documentation anywhere about what it does.
It really seems like i should know this since I’ve been coding for a while now but i never once used %… Does anybody know??

And if somebody does and has used it, what’s it good for?

I personally call it the modulus operator, though they may be alternative names
The modulus operator returns the remainder of division

local a = 10
local b = 2

print(a / b) -- 5
print(a % b) -- 0

(if you’re curious whats under the hood)

function remainder(a, b)
    return a - math.floor(a/b)*b
end

print(remainder(10, 2)) -- 0
6 Likes

It’s called the modulus operator. X%Y returns the remainder of x divided by y. I commonly use it to check if numbers are even or odd. X % 2 returns 0 if X is even and 1 if X is odd.

6 Likes