6

Possible Duplicate:
Integer division in JavaScript

Hopefully this is a simple question, basically I need to do this:

divider = 15

number = 50

Obviously 15 can be divided into 50 3 times with a remainder of 5, is there a simple way I can achieve this with math?

Obviously just dividing 50 by 15 will give me a rounded figure which I just want the lowest possible result and if there is anything left over and it's less than 15 just leave it alone.

Any help?

Cheers, Shannon

EDIT:

Thanks to Adil:

x = 50;
y = 15;
res = x % y;
x = (x - res) / y;
// x = 3
Community
  • 1
  • 1
Shannon Hochkins
  • 11,763
  • 15
  • 62
  • 95

2 Answers2

14

You can use modulus operator % to get remainder after division.

Live Demo

remainder = 50 % 15;
Mark Dickinson
  • 29,088
  • 9
  • 83
  • 120
Adil
  • 146,340
  • 25
  • 209
  • 204
  • 1
    You should get 3 as x-res = 45 and dividing 45 by 15 gets you 3, http://jsfiddle.net/rajaadil/XNhec/1/ – Adil Dec 27 '12 at 11:32
0

To get the left over use the modulo. (division remainder)

x= 50 % 15
Hugo Delsing
  • 13,803
  • 5
  • 45
  • 72