0

How can I round a number entered into a text box and divided by 18, UP regardless of whether its below the .5 (with Javascript/jQuery). Currently I am using:

nopallets=parseInt(howmany/18);

But this rounds either down or up depending on which integer it is closest to.

Any help greatly appreciated.

DrColossos
  • 12,656
  • 3
  • 46
  • 67
Dom
  • 2,275
  • 3
  • 24
  • 34
  • 1
    The answer is available on SO: http://stackoverflow.com/questions/5191088/how-to-round-up-a-number-in-javascript – Alfabravo Jul 19 '12 at 16:55
  • why on earth was my question downvoted? :/ that's just silly, the link provided does not answer my question so I haven't re-posted a question that's already been asked. – Dom Jul 19 '12 at 18:10
  • Two things. 1. I didn't downvote (so chill out, mate). 2. Ceil is the answer, here and in the linked question. – Alfabravo Jul 19 '12 at 21:11

4 Answers4

5

Math.ceil(howmany/18) is what you want.

MDN Docs ceil returns the smallest integer greater than or equal to a number.

epascarello
  • 204,599
  • 20
  • 195
  • 236
2

http://www.w3schools.com/jsref/jsref_ceil.asp

nopallets=Math.ceil(howmany/18);
IAmBatman
  • 687
  • 1
  • 10
  • 20
  • Guys, many thanks for this answer and it was correct but also.. [w3fools.com](http://www.w3fools.com) – Dom Jun 08 '13 at 14:18
2
nopallets= Math.ceil(number);

it takes the ceiling of the number

Evan
  • 5,975
  • 8
  • 34
  • 63
2

Math.ceilwill do that for you.
See http://www.w3schools.com/jsref/jsref_ceil.asp for reference.

nopallets=Math.ceil(howmany/18);
bardiir
  • 14,556
  • 9
  • 41
  • 66
  • Guys, many thanks for this answer and it was correct but also.. [w3fools.com](http://www.w3fools.com) – Dom Jun 08 '13 at 14:18