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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.