-1

Possible Duplicate:
Integer division in JavaScript

Lets say I have a specific number.

B = 350

And I have elements depending on that number, but they all have different amounts of that number in them, like:

c = 351,
d = 710,
e = 1100

What I want is to be presented by the leftover from dividing the c, d, e with B x times. i.e.

c = 1,
d = 10,
e = 50
Community
  • 1
  • 1
Philip
  • 6,827
  • 13
  • 75
  • 104
  • 3
    Have you tried the modulo operator? `351 % 350 = 1; 710 % 350 = 10; 1100 % 350 = 50;` – jonhopkins Jan 09 '13 at 13:51
  • 1
    Keep in mind that in JavaScript, % is the remainder, not the modulo operation, i.e. `-2 % 5` gives `-2` instead of `3`. – phant0m Jan 09 '13 at 14:16

2 Answers2

3

Use the % (Modulus) Operator.

var B = 350, c = 351, d = 710, e = 1100;

console.log(c%B);
console.log(d%B);
console.log(e%B);

Taken from mdn:

The modulus operator returns the first operand modulo the second operand, that is, var1 modulo var2, in the preceding statement, where var1 and var2 are variables. The modulo function is the integer remainder of dividing var1 by var2. For example, 12 % 5 returns 2. The result will have the same sign as var1; that is, −1 % 2 returns −1.

FIDDLE

Chase
  • 29,019
  • 1
  • 49
  • 48
2

Modulus % operator does it:

var B = 350,
    c = 351, d = 710, e = 1100;

console.log(c % B, d % B, e % B);  // 1 10 50
VisioN
  • 143,310
  • 32
  • 282
  • 281