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.

Get jQuery and JavaScript Phrasebook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.