What does % mean?

Just a Quick Question, what does % mean exactly?

ive seen scripts where they use % to see if a number is even such as return (num % 2)

Im pretty sure it means if a number can be divided into another one without a remainder but i just want to be sure

6 Likes

% is the modulus/modulo operator. It gives the remainder of 2 numbers for example:

10%2 = 0
5%3 = 2

3 Likes

This (%) is a modulus. These return the decimal of an mathematical operation you did.
Say that i did 10 Ă· 2 usually. This would give 5. But if i did 10 % 2 it would give 0. It simply returns the decimal from said operation.
Here’s a little script to visualize :

print(10 / 2) --prints 5
print (10 % 2) --prints 0

If you need to round things, modulus isn’t for that.

There’s also a case where you see these in things like these :

local newStr = "This is an example. 12345!"
local str = string.gsub(newStr, "%d", "678910!"
print(str) --will print out "This is an example. 678910!"

If you aren’t familiar with this, simply focus on the modulus here : it can be used in a more complex topic (a.k.a string functions) in a string pattern, or even as a specifier for other more complicated topics (like os.date, for example)

I recommend you not to jump into the things I just mentioned, I only explained that other part for when you figure out all the rest of Lua until this point.

2 Likes

I believe that this reply is wrong, and I advise you do NOT take this advice. Given the context of this topic, % means

I know about the context, and i explained why i said the other part. The beginning part is for his general question. The other is in case he learns more about Studio (i even mentioned the things % can be used for, like string functions, os.date, etc.) so that they could come back to this post later, in case if they need to. :disappointed:
My last sentence said so, anyway.

1 Like

Your first answer about it removing the decimal is a misconception. The correct answer is that it is modulo, and returns the remainder of the division:

print(10 / 5) --> 2
print(10 % 5) --> 0

1 Like

Aaaaah. I’ll edit it then, thank you!

1 Like

thanks everybody for explaining, i only really used this once but i couldnt find anything on google about it so i just decided to make a post lol

1 Like

It’s the modulo operator and it returns the remainder of an integer division.

An integer division is a division without decimal points. For example the integer division of 10/3 is 3(instead of 3.333...) and the remainder/modulo is 1(because num-divider*result = 10-3*3 = 1). Many languages support integer division by default(for example in python we do // and in C if two variables are of type integer the division is an integer division). However in lua we need to combine normal division with the math.floor function:

local integerDivision = math.floor(10/3)
print(integerDivision) --3
print(10%3 == 10-3*integerDivision) --true

Modulo is useful for a number of things, including converting things from a base to another(for example from base-10 to binary, base-8 or hex), for pseudorandom numbers, for certain things in cryptography and for running specific code on each x steps, for example if i%2 == 0 then task.wait() end should yield code inside a for loop once per two cycles, i%3 once per three, etc.

It’s also worth noting that the result of a modulo operation is always between 0 and modulo-1. So x%2 only returns 0 or 1, x%3 only 0, 1 or 2, etc.

1 Like

You know how while dividing on paper you get a remainder? Something like:
image
(Pardon for toddler like handwriting LOL)

Where the 1 is the answer and 2 is the remainder? Basically the “%” or modulus operator does the same. It gets the remainder. For example:

print(5%3) -- Output: 2

Where 3 is the divider and 5 is the value.

It is usually used to limit numbers, for example, let’s say you have an angle of 400. You want to convert it into a range of 0-360. How would you do that? You use the modulus operator!

local angle = 400
local convertedAngle = angle%360
print(convertedAngle) -- Output: 40

Hope I clarified this in the easiest way possible!

2 Likes

% marks the modulus/modulo operator or the format of a string.

Modulus/Modulo:

x % y is essentially just the remainder of x / y. If x is larger than y, return x.

print(100 % 2) --> 0
print(5 % 2) --> 1
print(2 % 5) --> 2
print(99 % 109219) --> 99

Format Strings

Using some functions of the string library, you can use % to format a string in a way.

print(string.format("helloooooo %s!", "OnlyTwentyCharacters")) --> helloooooo OnlyTwentyCharacters!
--%s means a string.
--%i means an integer.
--%d means a number of any type.
--%% returns % itself.
--etc.
local perc = 57
print(string.format("%i%%", perc)) --> 57%
1 Like