-1

I know this is a very basic question (I'm relatively new to C#), but somehow I can't find anything about it on the internet.

I just would like to understand what the "%" operator does, I've seen applications before and have a slight idea but am not 100% sure what exactly it does.

For example, here is a method that is executed every 10 frames (in Unity):

void Update() {
     frames++;
     if (frames % 10 == 0) {
         // method
     }
}

And here is a game time calculation that is displayed:

// time = incremented by 1 every second

int seconds = (time % 60);
int minutes = (time / 60) % 60;
int hours = (time / 3600) % 24;
// int days = (time / 86400) % 365;

What exactly does the % operator do in these examples and how can I best understand it.

David
  • 75
  • 1
  • 9
  • 1
    https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators#remainder-operator- – Steve Dec 19 '20 at 12:25
  • Maybe I should have dug a little deeper (just open a website that explains each operator, like the link you sent me). But still, I don't really understand what "the remainder after dividing" means. Why does "5 % 4" have an output of 1? If you were to divide those two, it would come out to 1.25, so whats the "remainder"?. I'm not the best at math (and maybe English??), so Im afraid it's a bit hard for me to understand :( – David Dec 19 '20 at 12:33
  • 2
    No @DavidB, Think about it as, how many times is 4 contained in 5? 1 times with 1 as remainder. So in your first example, the if becomes true for every value of _frames_ that is an exact multiple of ten. – Steve Dec 19 '20 at 12:38
  • The operation is called "modulo" (No one mentioned that yet) – Dennis_E Dec 19 '20 at 12:41
  • [Division and Remainders](https://www.mathsisfun.com/numbers/division-remainder.html) – ProgrammingLlama Dec 19 '20 at 12:45
  • Ah now I understand, thank you @Steve! @Dennis_E also many thanks, with the name of the operator I find much more in the Intenet (+ examples). Thanks to both, you have helped me a lot :) – David Dec 19 '20 at 12:47
  • Thanks @John! Apparently I think too complicated to understand kindergarten "calculations" xD silly me – David Dec 19 '20 at 12:48
  • @DavidB I didn't mean to imply anything :) I just thought that it had good worked examples of how the remainder is arrived at. – ProgrammingLlama Dec 19 '20 at 12:50

3 Answers3

5

The % operator returns the remainder of a division.

In the first example the code will be executed every 10 frames because the remainder of number of frames divided by 10 will be 0 only every 10th frame.

In the second example time is total number of seconds, so if you do time % 60 you will get number of seconds in the last minute, because minute has 60 seconds and so the remainder of time / 60 is number of seconds in the last minute.

Basically num1 % num2 is same as num1 - (floor(num1 / num2) * num2) where floor is rounding down.

Bonny4
  • 271
  • 3
  • 6
4

As the other comments it stands for modulo and by this operator, you get the remainder of a division.

const quotient = 11 / 4;
// quotient will be 2;

=> because; 4+4+3 (there is two 4s in 11)

const remainder = 11 % 4;
//remainder will be 3; 

=> because; 4+4+3 (after two 4s, remainder will be 3)

Another practical use of the modulo operator is that you can determine a number whether it is an odd or even number, as below.

var myNumber = 5;
if (myNumber % 2 == 0) {

 Console.WriteLine("Mynumber is an even number.");

} else {

 Console.WriteLine("Mynumber is an odd number.");

}
0

The % operator returns the remainder of a division.

See: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators#remainder-operator-

Max
  • 9,220
  • 10
  • 51
  • 83