-1

I'm trying to convert some JavaScript code into Java, but I do not know what the % character between two numeric variables does:

    testvalue = somevalue%anothervalue;

What does this mean or what would the same statement be in Java?

I also have this in JavaScript:

    if(somevalue%2 == 1){
    }

What does %2 mean here?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Mark
  • 1,186
  • 2
  • 16
  • 34
  • 1
    @jlordo, it seems he is translating from one to another. I do not think there is any confusion. – jn1kk Feb 06 '13 at 14:46
  • And I believe it is the modulo operator OP. – jn1kk Feb 06 '13 at 14:46
  • 5%2 is 1. It gives remainder. – srijan Feb 06 '13 at 14:47
  • @jsn: I was too quick. Should have read more carefully. – jlordo Feb 06 '13 at 14:48
  • 1
    Have you searched for JavaScript math operators? I recently needed to know if JS had this operator an that search gave me very straightforward results. I won't answer, however, because I think there's a good fish vs fishing opportunity here. – Carl Feb 06 '13 at 14:49

7 Answers7

5

It's the modulus operator.

This returns "the first operand modulo the second operand", which is the remainder when you substract (or add) as much of the second operand off/to of the first, to get as close as possible to 0.

Here's an example:

 5  % 2 ==  1  ( 5  = 2*2   +1)
 6  % 2 ==  0  ( 6  = 2*3   +0)
 12 % 5 ==  2  ( 12 = 5*2   +2)
-5  % 2 == -1  (-5  = 2*-2  -1)
-6  % 2 ==  0  (-6  = 2*-3  -0)
-12 % 5 == -2  (-12 = 5*-2  -2)
//                           ^ That's the result of the modulo.       
T.Galisnky
  • 29
  • 8
Cerbrus
  • 60,471
  • 15
  • 115
  • 132
  • your description does not make sense for negative numbers. – Alex Feb 06 '13 at 14:48
  • @Alex: And "Division remainder" does? `5 / 2 == 2.5`. The other answers aren't any less vague than mine was. Any way, edited. – Cerbrus Feb 06 '13 at 14:56
3

It is the modulus operator. The modulus operator performs division on the two values and gives you the remainder as the output.

For example:

var testValue = 5 % 3;

testValue output would be 2

Josh Mein
  • 26,372
  • 12
  • 72
  • 84
0

It is the modulo operator. It gives you the remainder of somevalue/anothervalue, i.e. 10%4 === 2.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Alexander Gessler
  • 42,787
  • 5
  • 78
  • 120
0

It's the modulo operator in both Java and JavaScript and other programming languages.

It returns the remainder of a division, e.g.,

5 % 2 = 1
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
stacker
  • 64,199
  • 27
  • 132
  • 206
0

It's "modulus" or remainder division - in other words, returning the remainder of the division.

See this page on JavaScript Operators.

Mike
  • 2,119
  • 3
  • 20
  • 32
0

It performs a modulo calcuation. That is: determining the divison reminder. And yes, Java uses the same syntax for this.

Example: 5%3 = 2

Because 3 goes into 5 once, and there is a reminder of 2.

Josh Mein
  • 26,372
  • 12
  • 72
  • 84
Marc Fischer
  • 1,296
  • 12
  • 16
0
X%5

means the remaining of x/5 so if x=12 then x%5 = 2 if you want to increase a number in a loop up to a maximum

<button onclick="incX();">increase up to 5</button>
<script type="text/javascript">
    var x=0;
    function incX(){
        x%=5;
        x++;
        alert("X = "+x);
    }
</script>
George SEDRA
  • 680
  • 8
  • 10