Round a float up to the next integer in javascript

Heba Gomaah picture Heba Gomaah · Jun 25, 2012 · Viewed 51.2k times · Source

I need to round floating point numbers up to the nearest integer, even if the number after the point is less than 0.5.

For example,

  • 4.3 should be 5 (not 4)
  • 4.8 should be 5

How can I do this in JavaScript?

Answer

Peter Olson picture Peter Olson · Jun 25, 2012

Use the Math.ceil[MDN] function

var n = 4.3;
alert(Math.ceil(n)); //alerts 5