O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Rounding Numbers

var x = 3.4; var y = 3.5; Math.round(x); //returns 3 Math.round(y); //returns 4 Math.ceil(x); //returns 4 Math.ceil(y); //returns 4 Math.floor(x); //returns 3 Math.floor(y); //returns 3

The JavaScript Math object provides three methods to round numbers. The .round() method rounds numbers #.5 up to the next higher integer and numbers #.49999... down to the next lowest integer. The .ceil() always rounds up to the next highest integer. The .floor() always rounds down to the next lowest integer.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required