-4

I understand that the code is convert miliseconds to seconds, mins and hours, but I dont understand what the " % " does ... int seconds = (int) (milliseconds / 1000) % 60 ;

can someone explain please?

can I do the same action in C++ ? thanks !!

             milliseconds = ((System.currentTimeMillis()) - (startTime));
            int seconds = (int) (milliseconds / 1000) % 60 ;
            int minutes = (int) ((milliseconds / (1000*60)) % 60);
            int hours   = (int) ((milliseconds / (1000*60*60)) % 24);
Helena
  • 81
  • 2
  • 2
  • 5

1 Answers1

4

% is the Modulus operator. For Java Modulus:

"%  Modulus - Divides left hand operand by right hand operand and returns remainder"

For example: 10 % 3 is equal to 1. To visually see this -

10 % 3
10 - 3 = 7    // Start by subtracting the right hand side of the % operator
7 - 3 = 4     // Continue subtraction on remainders
4 - 3 = 1
Now you can't subtract 3 from 4 without going negative so you stop.
You have 1 leftover as a remainder so that is your answer.

You can think of it as "How much would I have to subtract to the value on the left in order to make it evenly divisible by the right hand value?"


And yes, actually it's the same symbol in C++ for modulus.


"In arithmetic, the remainder is the integer "left over" after dividing one integer by another to produce an integer quotient (integer division)."

"In computing, the modulo (sometimes called modulus) operation finds the remainder of division of one number by another."

Andrew_CS
  • 2,542
  • 1
  • 18
  • 38
  • I read it too in some site .. but still I dont understand – Helena Jul 19 '14 at 11:14
  • @Helena Do you know what a remainder is? – Joseph Mansfield Jul 19 '14 at 11:18
  • 1
    @Helena: if you divide a whole number (aka "integer") like `17` by `5`, the result is `3`, but there is a rest of `2`, because `17 = 5*3 + 2`. That "rest" is the remainder, and that is what `%` returns. In other words: `17 / 5 --> 3`, `17 % 5 --> 2`. – Rudy Velthuis Jul 19 '14 at 11:26
  • 3
    if you don't understand what a modulus/remainder is, I suggest reading a basic math book instead of asking here – phuclv Jul 19 '14 at 11:30
  • thank you :) so it's the amount that remains after division.. right? – Helena Jul 19 '14 at 11:30
  • "The amount that remains" is what remainder means - so yes. – Andrew_CS Jul 19 '14 at 11:31
  • 1
    @LưuVĩnhPhúc: I am Dutch, and I am sure many of my countrymen may know the concept of a remainder or modulus, but do perhaps not know the English terms "remainder" or "modulus". In Dutch, it is generally called "rest", and in some other languages too. – Rudy Velthuis Jul 19 '14 at 11:32
  • @LưuVĩnhPhúc , I am not a native English speaker, the word "remainder" very similar to "reminder"... now I understand what is remainder. – Helena Jul 19 '14 at 11:32
  • @Helena I added a couple of definitions for clarification. – Andrew_CS Jul 19 '14 at 11:35
  • @Andrew_CS thank you again, you are a good man =]] – Helena Jul 19 '14 at 11:51